let arrObj = [
{ name: "小红", id: 1 },
{ name: "小橙", id: 1 },
{ name: "小黄", id: 4 },
{ name: "小绿", id: 3 },
{ name: "小青", id: 1 },
{ name: "小蓝", id: 4 }
];
function fn3(tempArr) {
let result = [];
let obj = {};
for (let i = 0; i < tempArr.length; i++) {
if (!obj[tempArr[i].id]) {
result.push(tempArr[i]);
obj[tempArr[i].id] = true;
};
};
return result;
}
fn3(arrObj)
let uniqueMap = new Map();
arrObj.forEach(item => {
uniqueMap.set(item.id, item);
});
let uniqueArr = Array.from(uniqueMap.values());
const uniqueArr = arrObj.reduce((unique, o) => {
if (!unique.find(obj => obj.id === o.id)) {
unique.push(o);
}
return unique;
}, []);