Wherefore art thou advice

Tell us what’s happening:
I am having an issue for when the source has more than one property, the object.keys method returns both property names as an array. But I need to be able to check each of the properties listed in the source, inside of the collection separately instead of checking each object for [‘apple’, ‘bat’]. How can I do this? Then I would also need to check the value of each property that matches with the source as well…

Your code so far


function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
let y = Object.keys(source)
console.log(y)
arr = collection.filter(item => item.hasOwnProperty(y) && item[y] === source[y])
// Only change code above this line
console.log();
}

whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 });

Your browser information:

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

Challenge: Wherefore art thou

Link to the challenge:

y is an array
so you need to loop through that array inside the callback of filter to check all the keys

this challenge is requiring you to apply many of the things you learned, and combine them in the right way

But when there are multiple numbers or strings in the array, when using a for loop, the filter function will only check one at a time. How can I make sure that this function will actually filter out the properties that contain all of the things in the array instead of including any properties in the object that are contained in the source? Without knowing how long the source array will be, I cannot hard code saying if(source[a] === item [y] && source[b] === item[y] etc…)

you can check the properties inside the function callback by looping on the array of keys, if there is one missing the callback should return false, if all are present it returns true

This is what I have now. For some reason, it keeps on returning a blank array. I have a feeling it is because the function returns false at some point, and then just reverts back to it’s original value of the blank array.

function whatIsInAName(collection, source) {

var arr = ;

// Only change code below this line

let y = Object.keys(source)

arr = collection.filter(function(item){

for(var i in y){

    let x = y[i]

    if(item.hasOwnProperty(x) && item[x] === source[x]){

      return true

    } else{

      return false

    } console.log(arr);

} 

})