JavaScript对象数组去重
//方法一
let obj =[
{
key: "nn",
age: 2
},
{
key: "name",
age: "asd"
},
{
key: "nn",
age: 2
}
]
let object = {};
let objres = obj.reduce((item,next) => {
object[next.key] ? "" : object[next.key] = true && item.push(next);
return item;
},[ ]);
//方法二
function uniq(arr,key){
for(let i=0;i<arr.length;i++){
for(let j=i+1;j<arr.length;j++){
if(arr[i]['key']===arr[j]['key']){
arr.splice(j,1)
j=j-1
}
}
}
return arr;
}