关于javascript:数组按id分类

10次阅读

共计 484 个字符,预计需要花费 2 分钟才能阅读完成。

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'},
       {id: '4', name: 'test3', rName: 'the1'},
       {id: '5', 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('2222', list2)
正文完
 0