1
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' }
];
2
  • what is the expected output? Commented Oct 29, 2022 at 6:28
  • brnad is not brand Commented Oct 29, 2022 at 6:32

3 Answers 3

3

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)

Sign up to request clarification or add additional context in comments.

7 Comments

I have added my exptected output on my question. Can you check it please?
@Shibbir please see my edit, that matches your required output
@Shibbir good to hear! :)
one more help. Can I remove the keys which value is empty?
@Shibbir sorry, I misinterpreted what you wanted. I thought for that sample data you would want { id : 18 } in the output instead of { id : '' }, not to remove id completely. In that case you should add the filter on result instead i.e. const result = obj.filter((o, i) => keys.lastIndexOf(Object.keys(o)[0]) == i).filter(o => Object.values(o)[0])
|
1

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 )

4 Comments

I have added my exptected output on my question. Can you check it please?
@Shibbir I change brnad to brand, you can check it now again
Can you please check my exptected output on my questions?
@Shibbir so you want to the latter key to override previous,in this case forEach might be a better choice or as Nick's answer
0

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)

1 Comment

id should be 18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.