Tell us what’s happening:
Hi, here I have chosen to loop through the collection, and find the items that have the same properties as source does, before comparing the values of the properties, and push it into the arr if they are the same. I’m not sure what went wrong here. the returned arr contains only the property pairs that matched, instead of the collection item that contains the matched pairs.
Can someone help me? thanks
Your code so far
function whatIsInAName(collection, source) {
// What's in a name?
let arr = [];
// Only change code below this line
for(let i in source){
for(let j = 0; j < collection.length; j++){
if(collection[j].hasOwnProperty(i)){
if(collection[j][i]===source[i]){
arr.push(collection[j])
}
}
}
}
console.log(arr)
// Only change code above this line
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.
Part of you the problem is that you are pushing the collection if one of the source property/value pairs is in the collection and not all the source property/value pairs are in the collection.
If you look at the first case where your function would fail (below).
Why? Let’s walk through your code with this example step by step of the for loops.
Does {“apple”:1,“bat”:2} have a property named “apple”?
Yes, {“apple”:1,“bat”:2} has “apple” property.
Does collection[“0”][“apple”] (1) match source[“apple”] (1) ?
Yes, it does, so add {“apple”:1,“bat”:2} to final array.
Does {“bat”:2} have a property named “apple”?
No, go to next collection
Does {“apple”:1,“bat”:2,“cookie”:2} have a property named “apple”?
Yes, {“apple”:1,“bat”:2,“cookie”:2} has “apple” property.
Does collection[“2”][“apple”] (1) match source[“apple”] (1) ?
Yes, it does, so add {“apple”:1,“bat”:2,“cookie”:2} to final array.
Does {“apple”:1,“bat”:2} have a property named “bat”?
Yes, {“apple”:1,“bat”:2} has “bat” property.
Does collection[“0”][“bat”] (2) match source[“bat”] (2) ?
Yes, it does, so add {“apple”:1,“bat”:2} to final array.
Does {“bat”:2} have a property named “bat”?
Yes, {“bat”:2} has “bat” property.
Does collection[“1”][“bat”] (2) match source[“bat”] (2) ?
Yes, it does, so add {“bat”:2} to final array.
Does {“apple”:1,“bat”:2,“cookie”:2} have a property named “bat”?
Yes, {“apple”:1,“bat”:2,“cookie”:2} has “bat” property.
Does collection[“2”][“bat”] (2) match source[“bat”] (2) ?
Yes, it does, so add {“apple”:1,“bat”:2,“cookie”:2} to final array.
When I solved this challenge, my outer loop was the collection itself and the inner loop went through each of the property/value pairs. I used a Boolean flag variable before beginning the inner loop and assumed that all the source property/value pairs would be found. To do this, I set it to true. Then when looping through each source property/value pair, if I found a situation where there was not a match, I would change the flag variable to false and break out of the inner loop. Then, all I had to do was check if the flag variable was true and if so, add the collection.
I have described a basic algorithm to solve this challenge (12 - 13 lines of code). There is another way which only takes 1-2 lines of code, but uses the filter and every methods.
Hi, thanks for the detailed reply. I have used another method to solve this problem. But this time something else has gone wrong. Here I try to turn both source and collection into arrays, so that I can check if any arrays in collection are identical to those of the source. I will turn them back into objects again when returning. But the filter method here is not working. The console has given a hint, but I do not understand what it means. Much appreciated if you had any advice!