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

205
Views
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 answers
Answer question

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 Report

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 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!