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

176
Views
how to check for same objects in two different arrays in JavaScript

i want to do something like this,

let array1 = [{obj1}, {obj2},{obj3}] 
let array2 = [{obj1}, {obj4},{obj5}]

output should be like

{obj1}

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This could work for simple objects.

Take in mind that it will not work for functions based properties.

const array1 = [{a:1}, {b:2},{c:3}] 
const array2 = [{a:1}, {d:4},{e:5}]

const array1Stringify = array1.map(el => JSON.stringify(el));
const array2Stringify = array2.map(el => JSON.stringify(el));

const result = array1Stringify.filter(el => array2Stringify.includes(el)).map(el => JSON.parse(el));

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

As commented, most of your problem is to define how your objects equality will be evaluated. Once you get that resolved, simply checking for the matches of one array in the other shold give you the matches you are after. Most readable and naive way, with a double for.

let array1 = ['hello', 'world', 'I rule'] 
let array2 = ['hello', 'whatever', 'hey brother']

let matches = [];
for (let i = 0; i < array1.length; i++) {
    for (let j = 0; j < array2.length; j++) {
        if (array1[i] === array2[j]) { //equality for object problem here
            matches.push(array1[i]);
        }
    }
}

console.log({matches});
about 4 years ago · Juan Pablo Isaza Report

0

  • Try this answer but you'll need an id or an specific object key value to compare https://stackoverflow.com/a/42995029/12128519
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!