Wherefore art thou.. code

Hello,
Could anybody tell me what’s wrong with the code, please??

Your code so far


function whatIsInAName(collection, source) {
  // What's in a name?

  // Only change code below this line

    return collection.filter(x => {
   return Object.keys(source).forEach(function(element) {  
return x.hasOwnProperty(element) && x[element] === source[element] ;
});
  });
  // Only change code above this line
}

console.log(whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 })); 

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/

The reason is your function only returns an empty array [] for each test case, so you just got lucky you passed the last one.

Why does you function always return an empty array? It is because the return statement inside your forEach method callback function just returns undefined (which is a “falsey” value). This means that a false value is being returned for each iteration of the filter method, so nothing is kept in the final array.

Hi Randell,
thanks for the comment
when I add

console.log(x.hasOwnProperty(element) && x[element] === source[element]) 

just above

return x.hasOwnProperty(element) && x[element] === source[element] ;

I get
“true”
“true”
“true”
so i am confused why it is undefined

Got it!
.forEach() - returns nothing
so in this case I should add something arr.push every cycle or replace forEach() for .every(), which returns a boolean

Hopefully you’ve gotten it to work.

Just a bit of unsolicited advice here (apologies), rather than throwing some built-in higher-order functions in the code and seeing if they’ll work, it helps to talk yourself through the problem and potential solution methods. In this problem, what are we doing?

Well, we have collection, which is an array of objects:
[Obj1, Obj2,...]

And, each object has a group of key-value pairs:
[{key1: value1, key2: value2, key3: value3,...}, {key1: value1, key2: value2, key3: value3,...}]

Also, we have source, which is an object with one or more key-value pairs:
{keyA: valueA, keyB: valueB,...}

So, if all of the key-value pairs in source can be found in an object in collection, that object should be included in our returned array (arr in this case).

In other words, for each object in collection, if every key-value pair in source can be found in that object, add it to our return array. How do we determine if every key-value pair in source is in the object? Well, let our condition be stated like this: for each key in source, if the object from collection has that key and a value paired with it that is equal to the value paired with that key in source, we are one step closer (count++, for example) to knowing whether or not we can push the collection object to our return array. If indeed, for each key-value pair in source we see that our condition is true, we can push the collection object to our return array.

Of course, if I were frustrated with such a problem and I came across a long-winded response like this one, I would want to throw a loaded beer can through the window of the one who posted it. But, after thinking it through, if I were to see the usefulness of it, I would be glad I saved that beer for a celebratory drink after posting the correct solution.