js对象数组去重

<script>
var array = [{
greeting: "Hello",
nickName: "Aziz"
}, {
greeting: "Hello",
nickName: "Aziz"
}, {
greeting: "Hello",
nickName: "test"
}];
let test = removeDuplicates(array, "nickName");
console.log(test);
function removeDuplicates(myArr, prop) {
return myArr.filter((obj, pos, arr)=>{
return arr.map(mapObj =>
mapObj[prop]).indexOf(obj[prop]) === pos;
});
}

var data = [{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];

var uniqueNames = [];
for(i = 0; i< data.length; i++){
if(uniqueNames.indexOf(data[i].website) === -1){
uniqueNames.push(data[i].website);
}
}

for(i = 0; i< uniqueNames.length; i++){
alert(uniqueNames[i]);
}

</script>

posted @ 2018-03-13 21:54  自由的鱼  阅读(392)  评论(0编辑  收藏  举报