js数组中对象去重 (reduce() 方法)

一个数组中含有对象,并且去掉数组中重复的对象。主要代码如下:

var arrData = [
    {id: 0, name: "小明"},
    {id: 1, name: "小张"},
    {id: 2, name: "小李"},
    {id: 3, name: "小孙"},
    id: 1, name: "小周"},
    {id: 2, name: "小陈"},
    
];
var obj = {};
var log = console.log.bind(console);
arrData = arrData.reduce((cur,next) => {
    obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
    return cur;
},[]) //设置cur默认类型为数组,并且初始值为空的数组
log(arrData);//打印出数组去重后的结果

 

posted on 2019-11-05 10:28  婧星  阅读(1562)  评论(0编辑  收藏  举报

导航