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

190
Views
Find matching object keys from an array of objects

Given an array of objects all with the same property names, with javascript how would you create a new object made up of key:value pairs that are found in all the objects of the array?

For example, given:

[
  {
    a: 'foo',
    b: 'bar',
    c: 'zip'
  },
  {
    a: 'urg',
    b: 'bar',
    c: 'zip'
  },
  {
    a: 'foo',
    b: 'bar',
    c: 'zip'
  }
]

Result:

{
  b: 'bar',
  c: 'zip'
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Start with all the elements of the first item (cloned, because we don't want to change the original data), then remove any key-value pairs that do not show up in the subsequent items:

const data = [
  {
    a: 'foo',
    b: 'bar',
    c: 'zip'
  },
  {
    a: 'urg',
    b: 'bar',
    c: 'zip'
  },
  {
    a: 'foo',
    b: 'bar',
    c: 'zip'
  }
];

const [first, ...rest] = data;
const result = rest.reduce((a, e) => {
  Object.keys(a).forEach(k => {
    if (!k in e || e[k] !== a[k]) {
      delete a[k];
    }
  });
  return a;
}, {...first});

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Just compare each value in the first object with every other object.

const [first, ...others] = [
  {
    a: 'foo',
    b: 'bar',
    c: 'zip'
  },
  {
    a: 'urg',
    b: 'bar',
    c: 'zip'
  },
  {
    a: 'foo',
    b: 'bar',
    c: 'zip'
  }
];


for (const [key, value] of Object.entries(first)) {
    for (const object of others) {
        if (object[key] !== value) {
            delete first[key];
        }
    }
}

console.log(first);
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!