js将数组中id相同的项重组成新的数组

var list = [

{ id: "1", name: "test1", rName: "the1" },

{ id: "1", name: "test1", rName: "the2" },

{ id: "1", name: "test1", rName: "the3" },

{ id: "2", name: "test2", rName: "the1" },

{ id: "2", name: "test2", rName: "the2" },

{ id: "3", name: "test3", rName: "the1" }

];

var list2 = [];

for (var i in list) {

var list3 = [];

for (var j in list) {

if (list[i].id == list[j].id&&list[j].key!=1) {

list[j].key=1

list3.push(list[j]);

}

}

if(list3.length>0){

list2.push(list3);

}

}

console.log(list2)

你可能感兴趣的:(js将数组中id相同的项重组成新的数组)