Js 数组根据ID去重
2024-03-31 本文已影响0人
Pluto_7a23
let list = [
{id:1,name:'张三’},
{id:2,name:'李四’},
{id:2,name:'李四’},
{id:3,name:'王五’},
]
利用reduce()方法
list = list.reduce(function(item, next) {
obj[next.id] ? '' : obj[next.id] = true && item.push(next);
return item;
}, []);