Ahhhhh okay, so object.values isn’t needed for this. Noted
function whatIsInAName(collection, source) {
let keysInSource = Object.keys(source);
return collection.filter(item => {
for (let i = 0; i < keysInSource.length; i++) {
if (item[keysInSource[i]] !== source[keysInSource[i]]) {
return false;
}
}
return true;
});
}
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));
Finally got it. Kind of feels like I cheated by looking at so many different things on the internet. Can’t wait until I get to a point where I’m getting through problems without so much help. But just to go over this solution one last time. I want to make sure I fully understand each line of code. It took me a long time because I honestly couldn’t think of putting a for loop within the filter, I don’t recall ever having to do that.
So this code is basically giving the keys of source a name. Then filtering the collection arrays so that if a item in the collection is not equal to the source…then it will return false, but if it is equal it will return that array as the answer
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.