JS数组对象根据某⼀相同key合并成新的数组
<script>
let labels = [{"id": 2, "name": "艾瑞", "label": "A"}, {"id": 4, "name": "⼤时代", "label": "D"}, {"id": 1, "name": "道合顺", "label": "D"}, {"id": 5, "name": "⼩⽶", "label": "X"},{"id": 3, "name": "知乎", "label": "Z"}]; function alpha_sort(labels){
js合并两个数组let alpha_dict = new Object();
let factories = new Array();
for(var i=0;i<labels.length;i++){
let factory = labels[i];
let label = factory.label;
if(alpha_dict[label] == undefined){
factories.push({"label": label, "children": [factory]});
alpha_dict[label] = factories.length -1;
}else {
factories[alpha_dict[label]].children.push(factory);
}
}
console.log(alpha_dict);
console.log(factories);
return factories;
}
console.log(alpha_sort(labels))
</script>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论