处理的效果如下
//初始值
let prev = [{
flag: "come on the way",
code: "abc"
},
{
flag: "come on the way",
code: "cba"
},
{
flag: "come on the way",
code: "cba"
}]
// 结果值
let result = [{
flag: "come on the way",
code: "abc"
},
{
flag: "come on the way",
code: "cba"
}]
1.使用filter和Map
function uniqueFunc(arr, uniId){
const res = new Map();
return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
}
2.使用reduce
function uniqueFunc(arr,uniId){
let obj = {}
return arr.reduce((prev,curr)=>{
obj[curr[uniId]] ? "" : obj[curr[uniId]] = true && prev.push(curr)
return prev
},[])
}