直接上代码:
var aa = [
{"foo": "22","name": "s"},
{"foo": "21","name": "sss"},
{"foo": "22","name": "sddd"},
{"foo": "25","name": "saaaa"},
{"foo": "22","name": "seqweqw"},
{"foo": "22","name": "sqweqw"}
];
/*
* 解决思路:
* 定义一个空对象(obj),将唯一的属性值放入这个对象。
* 在每次循环数组的时候,
* 如果当前数组对象中的某个属性值(这里我假设是“foo”)与obj的属性名不重复,
* 则将当前的对象添加到新的数组中。
*/
let removeItem = (arr) => {
let myarr = [], myobj = {};
arr.map((current,index)=>{
myobj[current.foo] ? '' : myobj[current.foo] = index && myarr.push(current);
});
return myarr;
}
let bb = removeItem(aa);
//[{"foo":"21","name":"sss"},{"foo":"22","name":"sddd"},{"foo":"25","name":"saaaa"}]