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

170
Vistas
Comparing two objects if they have at least one value in common

Given I have two objects, what is the simplest way to compare if they have at least one property matching (some or any)? I am not looking for a full match of a whole object A within object B but a match of partial object A within object B.

{
 name: "John",
 age: 23,
 city: "London"
}
// and 
{
 name: "Craig",
 age: 47,
 city: "London"
}
// returns true
{
 name: "Adrian",
 age: 32,
 city: "Sofia"
}
// and 
{
 name: "Tom",
 age: 44,
 city: "Oslo"
}
// returns false

I have tried several partial match functions by lodash (isMatch) but they seem to perform all-to-some or all-to-all matches, whereas I am looking for some-to-some.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you're simply checking if they have anything in common:

const john = { name: "John", age: 23, city: "London" }
const craig = { name: "Craig", age: 47, city: "London" }

const adrian = { name: "Adrian", age: 32, city: "Sofia" }
const tom = { name: "Tom", age: 44, city: "Oslo" }

function hasAnythingInCommon(obj1, obj2) {
  for (const key in obj1) {
    if(obj1[key] === obj2[key]) return true
  }
  return false
}

console.log(hasAnythingInCommon(john, craig))
console.log(hasAnythingInCommon(adrian, tom))

Short explanation:

  1. for (const key in obj1): Loop through all the keys in the first object.
  2. if(obj1[key] === obj2[key]) return true: If the two objects agree on the value they have on one of these keys, return true immediately.
  3. return false: If you get through the entire loop without returning true, then they have nothing in common, and we return false.
about 4 years ago · Juan Pablo Isaza Denunciar

0

The easiest way is to simply list out all attributes as comparisons:

const compare = (a, b) => a.name === b.name || a.age === b.age || a.city === b.city;

A more generic version would be:

const compare = (a, b) => Object.entries(a).some(([key, value]) => value === b[key]);
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