obj对象数据归类整理
Posted liubingyjui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了obj对象数据归类整理相关的知识,希望对你有一定的参考价值。
两个字段根据id对应整理
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>两个字段根据id对应整理</title> </head> <body> <script> var data ={ clation:[ {name: "高尔夫·纯电", parentid: "21"}, {name: "宝来·纯电", parentid: "21"}, {name: "蔚领C-TREK", parentid: "1"}, {name: "高尔夫 · 嘉旅",parentid: "1"}, {name: "宝来·传奇" ,parentid: "1"}, {name: "宝来" ,parentid: "1"}, {name: "高尔夫" ,parentid: "1"}, {name: "全新一代速腾" ,parentid: "1"}, {name: "迈腾" ,parentid: "2"}, {name: "CC" ,parentid: "2"}, {name: "探歌T-ROC" ,parentid: "3"}, {name: "探影TACQUA" ,parentid: "3"}, {name: "新迈腾" ,parentid: "2"}, {name: "探歌T-ROC" ,parentid: "3"}, ], level: [ {id: "3", name: "SUV"}, {id: "21", name: "新能源"}, {id: "2", name: "中型车"}, {id: "1", name: "紧凑车型"}, ] } var clation = data.clation; var level = data.level; console.log(data,‘before‘) var arr = []; for (var i = 0; i < level.length; i++) { if(level[i].id){ var obj = {}; obj.title = level[i].name; obj.list = clation.filter(function(e){ return (e.parentid ==level[i].id) }) arr.push(obj) } } //反选arr.reverse() console.log(arr,‘after‘) </script> </body> </html>
以上是关于obj对象数据归类整理的主要内容,如果未能解决你的问题,请参考以下文章