const list = [
{
id:1,
info: '西瓜'
},
{
id:1,
info: '西瓜'
},
{
id:2,
info: '草莓'
},
{
id:3,
info: '榴莲'
},
{
id:3,
info: '榴莲'
},
{
id:4,
info: '苹果'
}
]
const hash = {};
const arr = list.reduce(function(item, next) {
hash[next.id] ? '' : (hash[next.id] = true && item.push(next));
return item;
}, []);
数组的值是对象,然后通过reduce方法去掉ID重复的值,问题就是我感觉还是有点繁琐,现在的问题是如果别人看的话还需要理解一下子,有没有更简洁明了的写法,或者什么工具类比如lodash什么的方法推荐,谢谢