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

188
Views
Best way to find the difference between 2 arrays of objects

I have these 2 arrays of objects.

enter image description here

Sorry that I post the image because I want you guys to quickly understand my points.

What is the best way to compare 2 arrays of objects and look for what is not in it?

I can do 2 levels for loops and an if-check, but I'm not sure if it is the best approach. I am seeking a better performance JS way to achieve something like this.

This is what I am looking to extract while comparing arr1 & arr2.

{
    "id": 4,
    "name": "Date and Time"
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Different ways to achieve this requirement. I also created a performance check based on all the below 3 solutions here. Kindly run this test suite and based on the result you can use any of the below solution in your project.

  1. Filtered out the array by using Array.filter() method and find the id in the another array by using Array.find() method.

const arr1 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}];

const arr2 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}, {
    id: 4,
  name: "Date and Time"
}];

const res = arr2.filter((obj) => !arr1.find(({ id }) => obj.id === id));

console.log(res);

  1. By using Array.includes() method.

const arr1 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}];

const arr2 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}, {
    id: 4,
  name: "Date and Time"
}];

const IDArray = arr1.map(obj => obj.id);

const res = arr2.filter(obj => !IDArray.includes(obj.id));

console.log(res);

  1. Use Array.some() along with Array.filter() method.

const arr1 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}];

const arr2 = [{
    id: 1,
  name: "Weather"
}, {
    id: 2,
  name: "Geo Location"
}, {
    id: 3,
  name: "Device"
}, {
    id: 4,
  name: "Date and Time"
}];

const res = arr2.filter(({ id }) => !arr1.some(x => x.id == id))

console.log(res)

about 4 years ago · Juan Pablo Isaza Report

0

One way is to concatenate both arrays together, then iterate and filter to see if that id exists in both arrays. If it doesn't exist in both arrays, it's extra.

a1.concat(a2).filter(a => a1.findIndex(f => f.id === a.id) == -1 || a2.findIndex(f => f.id === a.id) == -1)

let arr1 = [{"id": 1,"name": "Date and Time"},{"id": 2,"name": "Date and Time"},{"id": 3,"name": "Date and Time"},{"id": 4,"name": "Date and Time"}]

let arr2 = [{"id": 1,"name": "Date and Time"},{"id": 2,"name": "Date and Time"},{"id": 3,"name": "Date and Time"}]

const findMissing = (a1, a2) => a1.concat(a2).filter(a => a1.findIndex(f => f.id === a.id) == -1 || a2.findIndex(f => f.id === a.id) == -1)

console.log(findMissing(arr1, arr2))

about 4 years ago · Juan Pablo Isaza 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!