如何将下列数组中key组成新的数组

var a=[{name:1,key:'a'},{name:2,key:'b'},{name:3,key:'a'},{name:3,key:'b'}]


怪盗饭团
浏览 1928回答 2
2回答

慕莱坞9220042

你好:var a=[{name:1,key:'a'},{name:2,key:'b'},{name:3,key:'a'},{name:3,key:'b'}]; var result = new Array(); for(var index in a){  result[a[index].key] = new Array();  result[a[index].key].push(a[index]); } console.log(result);望采纳!

weibo_青梅竹马酱_0

var b = {}; a.forEach(function (obj) {     var array = b[obj['key']] || [];     array.push(obj);     b[obj['key']] = array; }); console.log(b.a.length);console.log(b.b.length);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
JavaScript