JavaScript中数组的常用操作之数组去重
方法一
var color = ['red','blue','green','yellow','black','white','green','blue','pink','gold','cyan'];
//第一种方法
var uniqueColor = Array.from(new Set(color));
console.log(uniqueColor)
//第二种方法
var uniqueColor = [...new Set(color)]
console.log(uniqueColor)
方法二
const arr=[{id:1,value:'bob'},{id:1,value:'bob'},{id:1,value:'bob'},{id:1,value:'lucy'},{id:1,value:'lucy'},{id:2,name:'lucy'},{id:2,name:'张三'}]
const newArr = arr.reduce((result, item) => {
if (!result.find(x => x.value === item.value)) {
result.push(item);
}
return result;
}, []);
网友评论