let obj = [
{ id : 1 },
{ id : 10 },
{ brand : 12 },
{ id : 15 },
{ id : 18 },
{ image_link : 'some link' },
{ price : 10 },
{ brand : 111 },
{ image_link : 'some link 2' }
];
I have this array of object. I want filter this object so that I can get an object without duplicate keys.
I am trying this:
let uniqueIds = [];
let unique = obj.filter( (element ) => {
let key = Object.keys(element)[0];
let isDuplicate = uniqueIds.includes(element.key);
if (!isDuplicate) {
uniqueIds.push(element.key);
return true;
}
return false;
});
console.log( unique )
But everytime it's showing me :
[ { id: 1 } ]
My expected output:
[
{ id : 18 },
{ price : 10 },
{ brand : 111 },
{ image_link : 'some link 2' }
];
brnadis notbrand