Tell us what’s happening:
Problem is current object within collection is pushed to the array even if the current object doesn’t have ALL the properties in source object.
I believe it is because of my if then statement but i am unsure how i can restructure the .every(callback function) method.
Code is commented to the best of my ability.
Your code so far
function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
//each object in collection is filtered by callback function where "current" is the current object
collection.filter((current)=>{
/*
Object.keys(source): converts source object into array
.every(): array method (hence previous step). method tests whether all elements in the array pass the callback function
sourcekey: current key in source array (aka current property key in source object)
*/
Object.keys(source).every((sourcekey)=>{
//if current object has source's sourcekey AND current key value is equal to sourcekeys key value then
if((current.hasOwnProperty(sourcekey)) && (current[sourcekey] === source[sourcekey])) {
arr.push(current);//push current object into array
//THIS IS THE PROBLEM: even though this is within the .every() callback function, it pushes the current object even if not sourcekeys pass callback
//its becuase of the if then statement but unsure how to change
}
})
});
// Only change code above this line
return arr;
}
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));
//NOTE Object.is() won't work
//can't use dot notation because of variables
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
.
Challenge: Wherefore art thou
Link to the challenge: