Tell us what’s happening:
Hi Everyone,
For this problem, we are given a function with two arguments. The first argument (collection) is an array of objects and the second argument (source) is an object containing a list of properties (1 or more) and their associated values.
We want to filter our first argument and include only those objects which contain all of the properties and values of the second argument.
Where I am having trouble is with iterating through the second argument while at the same time iterating through the first argument, and checking for matching values.
If the second argument only contained one property, i could solve the problem as follows:
arr = collection.filter(x => x[Object.keys(source)] === source[Object.keys(source)])
I then tried the following code, but the problem it has is that it passes as true if the object contains any of the objects that we want.
let finalArr = [ ]
for(const x in source) {
finalArr = collection.filter(y => y[x] == source[x])
console.log(x)
}
console.log(finalArr)
I guess my question is, how do I get the code to run through all the properties in our second argument (source), and only pass true if all of those properties are contained with matching values. I feel like I’m on the right path, but maybe my brain is getting discombobulated with multiple itterations happening at the same time.
Your code so far
function whatIsInAName(collection, source) {
console.log(source)
var arr = [];
let finalArr = [];
for(const x in source) {
finalArr = collection.filter(y => y[x] == source[x])
console.log(x)
}
let newArr = [];
arr = collection.filter(x => x[Object.keys(source)] === source[Object.keys(source)])
console.log(arr)
return finalArr;
}
whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 });
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
.
Challenge: Wherefore art thou
Link to the challenge: