Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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())
about 4 years ago · Santiago Trujillo Report

0

use this code instead:

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

you cannot compare two objects because they always will be different because they are at different location in memory, you have to compare their properties or convert them to string and compare them

about 4 years ago · Santiago Trujillo Report

0

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

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

const findByPropertyDiff = (a,b)=>{
   let rs = a.filter(item=>{
     return Array.from(Object.keys(item)).some(k=>{
      return b.some(compared=>{
         return (compared[k] && item[k] != compared[k]);
      });       
     })
   });
   return rs;  
}

const updatedItem = findByPropertyDiff(x,y);
console.log(updatedItem);

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!