Shibbir
Shibbir

Reputation: 2031

JavaScript - Get the unique object from array of objects

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' }
];

Upvotes: 1

Views: 68

Answers (3)

Nick
Nick

Reputation: 147146

You can filter your array based on whether the object's key is the last occurrence of that key (checked using lastIndexOf) in the array:

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' }
];

const keys = obj.map(o => Object.keys(o)[0])

const result = obj.filter((o, i) => keys.lastIndexOf(Object.keys(o)[0]) == i)

console.log(result)

Upvotes: 3

Amila Senadheera
Amila Senadheera

Reputation: 13235

You have used element.key in two places it should be just key.

And since you need the final duplicate element you need to do a reverse of the input array and again do another reverse to unique array.

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' }
];

let uniqueIds = [];

// reverse the input array before start filtering
obj.reverse()

let unique = obj.filter( (element ) => {
    
    let key = Object.keys(element)[0];                
    let isDuplicate = uniqueIds.includes(key);

    if (!isDuplicate) {
        uniqueIds.push(key);
        return true;
    }
    return false;   
});
// to make the output order according to the input array
unique.reverse()
// to make the input array to the initial order 
obj.reverse()

console.log(unique)

Upvotes: 0

flyingfox
flyingfox

Reputation: 13506

The reason is that you need to use key instead of element.key

Note: it seems you have a typo brnad is not brand

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' }
];

let uniqueIds = [];
let unique = obj.filter( (element ) => {
    
    let key = Object.keys(element)[0];                
    let isDuplicate = uniqueIds.includes(key);

    if (!isDuplicate) {
       uniqueIds.push(key);
       return true;
    }
    return false;   
});

console.log( unique )

Upvotes: 1

Related Questions