Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if two arrays of objects have differences isolating the latter

I have two arrays of objects. Each object will have a unique property id which, for context, is a filterID retrieved from a dynamoDb database. What I want to achieve is, retrieving an array of the objects that are different given the same id.

const x = [
    {
        1234: "food"
    },
    {
        5678: "animal"
    },
    {
        4444: "car"
    }
]

const y = [
    {
        1234: "food"
    },
    {
        5678: "bread"
    },
    {
        4444: "car"
    }
]

const p = () => x.filter(object1 => {
    return !y.some(object2 => {
        return object1 === object2
    })
})
 console.log(p())

In this scenario I would be looking for something like

const different = [
    {
        5678: "animal"
    },
]

I have tried a few things like on the example above but so far no success.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re close, you just need to get the object key and compare the values, instead of comparing the object references.

const p = () => x.filter(object1 => {
    return !y.some(object2 => {
        const key = Object.keys(object1)[0];
        return object1[key] == object2[key];
    })
})
 console.log(p())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading