const arr = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 1, name: 'Alice' },
{ id: 3, name: 'Charlie' },
{ id: 2, name: 'Bob' }
];
const uniqueArr = arr.reduce((prev, cur) => {
const index = prev.findIndex(item => item.id === cur.id && item.name === cur.name);
if (index === -1) {
prev.push(cur);
}
return prev;
}, []);
console.log(uniqueArr);
// Output: [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }]
const arr = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 1, name: 'Alice'}];
const uniqueArr = Array.from(new Set(arr.map(obj => JSON.stringify(obj)))).map(str => JSON.parse(str));
console.log(uniqueArr); // [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}]
const arr = [{id: 1}, {id: 2}, {id: 3}, {id: 1}];
const result = arr.filter((obj, index) => {
return arr.findIndex(innerObj => innerObj.id === obj.id) === index;
});
console.log(result); // [{id: 1}, {id: 2}, {id: 3}]