I’ve used a different approach on solving a challenge and i’d like to share it, maybe it is going to help somebody…
link to the challenge :
solution:
function whatIsInAName(collection, source) {
// Only change code below this line
let arr = collection.filter((obj)=> {
let count = 0;//the counter to test
for (let key in obj) {//to loop through an object of
//of the collection array
for (let keysrc in source) {//to loop through
//the "source" object
if (key === keysrc && obj[key] === source[keysrc]) {
count++;//if the keys and the values are the same
//the counter increases by one
}
}
}
if (count === Object.keys(source).length) {
return true;//returns true, so the object on which
//the filter method is operating, stays in the
//collection array
}
});
// Only change code above this line
return arr;
}