Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda