js如何根据json数据中的某一个属性来给数据分组
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍js如何根据json数据中的某一个属性来给数据分组,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
如下所示:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<span id ="span" ></span>
</body>
<script>
var arr = [{"Group":1,"Groupheader":"质量管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""},
{"Group":1,"Groupheader":"","Leftimg":"","Left":"","Min":"质量巡检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"设备管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备专业点检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备日检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备周检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备月检","Right":"","Rightimg":""}];
var map = {},
dest = [];
for(var i = 0; i < arr.length; i++){
var ai = arr[i];
if(!map[ai.Group]){
dest.push({
Group: ai.Group,
data: [ai]
});
map[ai.Group] = ai;
}else{
for(var j = 0; j < dest.length; j++){
var dj = dest[j];
if(dj.Group == ai.Group){
dj.data.push(ai);
break;
}
}
}
}
console.log(JSON.stringify(dest));
var sapn = document.getElementById("span");
span.innerHTML = JSON.stringify(dest);
</script>
</html>
原始数据:
[{"Group":1,"Groupheader":"质量管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""},
{"Group":1,"Groupheader":"","Leftimg":"","Left":"","Min":"质量巡检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"设备管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备专业点检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备日检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备周检","Right":"","Rightimg":""},
{"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"设备月检","Right":"","Rightimg":""}];
处理后的数据:
[
{
"Group": 1,
"data": [
{
"Group": 1,
"Groupheader": "质量管理",
"Leftimg": "",
"Left": "",
"Min": "",
"Right": "",
"Rightimg": ""
},
{
"Group": 1,
"Groupheader": "",
"Leftimg": "",
"Left": "",
"Min": "质量巡检",
"Right": "",
"Rightimg": ""
}
]
},
{
"Group": 2,
"data": [
{
"Group": 2,
"Groupheader": "设备管理",
"Leftimg": "",
"Left": "",
"Min": "",
"Right": "",
"Rightimg": ""
},
{
"Group": 2,
"Groupheader": "",
"Leftimg": "",
"Left": "",
"Min": "设备专业点检",
"Right": "",
"Rightimg": ""
},
{
"Group": 2,
"Groupheader": "",
"Leftimg": "",
"Left": "",
"Min": "设备日检",
"Right": "",
"Rightimg": ""
},
{
"Group": 2,
"Groupheader": "",
"Leftimg": "",
"Left": "",
"Min": "设备周检",
"Right": "",
"Rightimg": ""
},
{
"Group": 2,
"Groupheader": "",
"Leftimg": "",
"Left": "",
"Min": "设备月检",
"Right": "",
"Rightimg": ""
}
]
}
]
以上是“js如何根据json数据中的某一个属性来给数据分组”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341