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

阅读 60

2022-01-23

(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)

0 0 举报