Tell us what’s happening:
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.
For example, if the first argument is [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }]
, and the second argument is { last: "Capulet" }
, then you must return the third object from the array (the first argument), because it contains the name and its value, that was passed on as the second argument.
Your code so far
function whatIsInAName(collection, source) {
var arr = [];
let secondObj = Object.entries(source);
for (let elem of collection){
let singleObj = (Object.entries(elem));
for (let testElement of secondObj){
for (let elem2 of singleObj){
console.log(elem2);
let string = testElement.toString();
let string2 = elem2.toString();
console.log(elem2, testElement, string2 === string);
if (string2 === string){
arr.push(Object.fromEntries(new Map(testElement)));
}
}
}
} return arr;
}
whatIsInAName([
{ first: "Romeo", last: "Montague" },
{ first: "Mercutio", last: null },
{ first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Why am I getting type error when I have done a fair amount of research on object comparison and I have applied to the above code? What is different above?
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15
.
Challenge: Wherefore art thou
Link to the challenge: