1、数组去重
uniq(array){
let temp = []; //一个新的临时数组
for(let i = 0; i < array.length; i++){
if(temp.indexOf(array[i]) == -1){
temp.push(array[i]);
}
}
return temp;
}
2、数组嵌套对象去重
var songs = [
{name:"羽根",artist:"air"},
{name:"羽根",artist:"air"},
{name:"晴天",artist:"周杰伦"},
{name:"晴天",artist:"周杰伦"},
{artist:"周杰伦",name:"晴天"}
];
unique(songs){
let result = {};
let finalResult=[];
for(let i=0;i<songs.length;i++){
result[songs[i].name]=songs[i];//因为songs[i].name不能重复,达到去重效果,且这里必须知晓"name"或是其他键名
}
//console.log(result);{"羽根":{name:"羽根",artist:"air"},"晴天":{name:"晴天",artist:"周杰伦"}} //现在result内部都是不重复的对象了,只需要将其键值取出来转为数组即可
for(item in result){
finalResult.push(result[item]);
}
//console.log(finalResult);[{name:"羽根",artist:"air"},{name:"晴天",artist:"周杰伦"}]
return finalResult;
}
console.log(unique(songs));