Wherefore art thou avoiding filter method

The code passes 3/6 tests. I have viewed some of the hints and other users solutions after many hours trying to figure this out as well as researching Objects vs. Array’s. I’m aware that the filter method can be used to solve this but I’d like to figure out why my code isn’t working.

Your code so far


function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
let obj = Object.keys(source)

for (let i = 0; i < collection.length; i++) {
if (collection[i][obj] == Object.values(source) === true) {
  arr.push(collection[i])  
}    

}

// Only change code above this line
console.log(arr);
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

first issue: your function doesn’t have a return statement

the first test that doesn’t pass is for function call whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }), let’s see what the issues are

what do you want to do with collection[i][obj]?
obj is an array, collection[i][obj] is undefined

Object.values(source) is also an array, you can’t compare an array to something else with a comparison operator, you need array methods for that. (console.log([1, 2] == [1, 2]) will print false!)

you also don’t need the === true at the end, as from a comparison you already get a boolean.

You are able to pass the first few tests only because the source object has only one property and there is a few coercions to string, but it is totally accidental. You need to change a few things to be able to pass the algorithm with an object with multiple properties