关于javascript:数组按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' },
       { 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)

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理