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

121
Views
How to get same values from 2 arrays with objects in ES6 Javascript?

So I have here two different arrays.

const test = [
    { firstName: "John", lastName: "Doe" },
    { firstName: "Michael", lastName: "Sins" },
    { firstName: "Alex", lastName: "Brown" }
  ];
const test2 = [
    { firstName: "Lisa", lastName: "Shore" },
    { firstName: "John", lastName: "Doe" },
    { firstName: "Justin", lastName: "Park" },
  ];

What I want is to get the same values from the two arrays using ES6 (filter if possible) or in any way that fits to achieve the output. The result that I want to get is :

[{ firstName: "John", lastName: "Doe" }]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can try using Array.prototype.filter() and Array.prototype.some()

const test = [
    { firstName: "John", lastName: "Doe" },
    { firstName: "Michael", lastName: "Sins" },
    { firstName: "Alex", lastName: "Brown" }
  ];

const test2 = [
    { firstName: "Lisa", lastName: "Shore" },
    { firstName: "John", lastName: "Doe" },
    { firstName: "Justin", lastName: "Park" },
  ];

const res = test.filter(t1 => test2.some(t2 => 
                    t1.firstName == t2.firstName 
                    && t1.lastName == t2.lastName
                  ));
console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

You can do:

const test = [{ firstName: 'John', lastName: 'Doe' },{ firstName: 'Michael', lastName: 'Sins' },{ firstName: 'Alex',lastName: 'Brown' }]
const test2 = [{ firstName: 'Lisa', lastName: 'Shore' },{ firstName: 'John', lastName: 'Doe' },{ firstName: 'Justin',lastName: 'Park' }]


const getFullName = ({ firstName, lastName }) => firstName + lastName
const test2Names = test2.map(getFullName)
const result = test.filter((o) => test2Names.includes(getFullName(o)))

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

Thi swill still works if you have object with properties other than firstName and lastName

const test = [
    { firstName: "John", lastName: "Doe" },
    { firstName: "Michael", lastName: "Sins" },
    { firstName: "Alex", lastName: "Brown" }
  ];
  
  const test2 = [
    { firstName: "Lisa", lastName: "Shore" },
    { firstName: "John", lastName: "Doe" },
    { firstName: "Justin", lastName: "Park" },
  ];
  
  const tempTest2 = test2.map(item => JSON.stringify(item));
  
  const result = test.filter(item => tempTest2.includes(JSON.stringify(item)));
  
  console.log(result);

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!