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

JavaScript012

js将数组中id相同的项重组成新的数组,第1张

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)

var arr = [

    {content: "页面设计", id:1, num:105,parentid:1},

    {content: "网站使用的便捷度", id:2, num:105,parentid:1},

    {content: "信息公开力度", id:3, num:105,parentid:1},

    {content: "网上互动服务", id:4, num:105,parentid:1}

]

for(var i = 0 i < arr.length i++){

    var obj = arr[i]

    for(var key in obj){

        console.log(key + ": " + obj[key])

    }

}

没有啥区别啊,具体也就是,所在的语言不一样的,js属于弱类型脚本语言,java属于面向对象编程语言,js中不用明确变量的类型,一般用var 声明,

js:for(var lt in list){} java: for(声明类型 lt :list){} js用关键字in,java不用in用:就可