js去除json对象数组中的重复元素

let data = [

  {id:"123",name:"a"},

  {id:"12",name:"b},

  {id:"123",name:"c"},

]

let userIds = [];

data.forEach(d => {
    //id:相同的去掉,去重
    if (userIds.indexOf(d.id) < 0) {
        userIds.push({
            id: d.id,
            name: d.name
        })
    }
})

userIds = [{id:"123",name:"a"},{id:"12",name:"b}]

你可能感兴趣的:(js去除json对象数组中的重复元素)