json数组去重 json数组去重

json数组去重

var a = [
{
"id":"1001",
"name":"张三",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
},
{
"id":"1002",
"name":"李四",
"age":"15",
"address":"北京市海淀区",
"school":"海淀区第二中学"
},
{
"id":"1003",
"name":"王五",
"age":"16",
"address":"北京市石景山区",
"school":"石景山区第二中学"
}
];
var b = [
{
"id":"1004",
"name":"小毛",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
},
{
"id":"1003",
"name":"王五",
"age":"16",
"address":"北京市石景山区",
"school":"石景山区第二中学"
}
]
去重后结果:
var c = [
{
"id":"1001",
"name":"张三",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
},
{
"id":"1002",
"name":"李四",
"age":"15",
"address":"北京市海淀区",
"school":"海淀区第二中学"
},
{
"id":"1003",
"name":"王五",
"age":"16",
"address":"北京市石景山区",
"school":"石景山区第二中学"
},
{
"id":"1004",
"name":"小毛",
"age":"18",
"address":"北京市朝阳区",
"school":"朝阳区第二中学"
}

];

var c = a.concat(b),//合并成一个数组
        temp = {},//用于id判断重复
        result = [];//最后的新数组
    //遍历c数组,将每个item.id在temp中是否存在值做判断, 
    c.map((item,index)=>{
        if(!temp[item.id]){
            result.push(item);
            temp[item.id] = true
        }
    })
    console.log(result)    


JS的数组去除重复。

//去除重复。 let _selectedIds2=[]; //最后的新数组; _selectedIds.map((item)=>{ //从这里开始匹配,如果没有匹配到,  if(_selectedIds2.indexOf(item)<0){ _selectedIds2.push(item);}
})
分享本文至:

评论 0

挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
提交评论

清空信息
关闭评论