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

206
Vistas
How to match objects with the same key-value pairs passed as arguments?

This is a FreeCodeCamp task named Wherefore art thou.

I have to Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.

My solution passes all the checks besides this one: whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3}) should return []. And I wonder why the function returns the given array instead of an empty one. And if there is a way to make my function work eventually. My code is gonna be below. THANK YOU FOR HELP!

function whatIsInAName(collection, source) {
  let arr = [];
  const sourceKeys = Object.keys(source);

  for (let key in sourceKeys) {
    arr = collection.filter(
    obj => (Object.keys(obj).length >= sourceKeys.length) ?
    obj.hasOwnProperty(sourceKeys[key]) 
    && obj[sourceKeys[key]] === source[sourceKeys[key]] : 0);
  }

  return arr;
}
whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could create a simple implementation using Object.keys() and Array.every().

This means each returned item must include each key and value from the source, but may include extra keys and values:

function whatIsInAName(collection, source) {
  return collection.filter(obj => Object.keys(source).every(key => obj[key] === source[key]));
}

console.log(whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3}));
console.log(whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 2, "c": 3}));
console.log(whatIsInAName([{"a": 1, "b": 2, "c": 3, "d": 4 }], {"a": 1, "b": 2, "c": 3}));
console.log(whatIsInAName([{"a": 1, "c": 3, "d": 4 }], {"a": 1, "b": 2, "c": 3}));
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

The following should work:

function fltArr(collection, p) {
  const tst=Object.entries(p);
  return [...collection].filter(o=>
    tst.every(([k,v])=>o[k]==v)) // check for each key and value of p
}                                // that they are the same in o[k]

const arr=[{a: 1, b: 2, c:3, d:7},{a: 1, b: 2, d: 3}],
 patterns=[{a: 1, b: 9999, c: 3},{a: 1, b: 2, c: 3},{a: 1, b: 2, d: 3}];

patterns.forEach(p=>console.log(fltArr(arr,p)))

As far as I understand the question, the collection object can have extra properties (not specified in p) and still be a match. For this reason the first element is a match with the second test pattern.

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