0
点赞
收藏
分享

微信扫一扫

数组对象中,去除对象中某字段且内容相同的对象

(1)去除id相同的对象 

  let  person = [
    { id:1 , text: 3},
    { id:2 , text: 4},
    { id:5 , text: 3},
    { id:1 , text: 2}  
  ];
    
  let obj = {};

  person = person.reduce( ( pre,cur ) => {
     obj[cur.id] ? "" : obj[cur.id] = true && pre.push(cur);
     return pre;
  },[]);

  console.log(person);

得出结果:

person = [
 {id: 1, text: 3}
 {id: 2, text: 4}
 {id: 5, text: 3}
]
举报

相关推荐

0 条评论