Tell us what’s happening:
I’m getting an arr.filter is not a function error. The last time I got this there was a typo where I typed .fiter() instead of .filter(). It doesn’t seem to be the case this time. Why does it throw an error?
Your code so far
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
collection.map(function (arr){
return arr.filter(function (arr){
if (arr.includes(source)){
return this
}
})
})
// Only change code above this line
return arr;
}
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/67.0.3396.99 Safari/537.36.
So I can’t even map it? i’m looking on mdn docs for objects and I see that I can convert it into a string but I can’t see anything to turn objects into arrays. Is the only way to loop through this a for loop?
when I log collection.keys() it logs an Array Iterator. If I use a for loop to loop through it it logs each value. I tried to use an if condition in the for loop to return values that contain the key in the source argument. It doesn’t appear to be working. How do you put replits in these replies?
function whatIsInAName(collection, source) {
// What’s in a name?
var arr = ;
// Only change code below this line
for (i=0; i<collection.length; i++){
console.log(collection[i]);
if(collection[i].hasOwnProperty(source)){
return collection[i];
}
}
console.log(collection.keys(collection))
// Only change code above this line
return arr;
}
No need to apologize.
I wanted to ask you if you’ve written up an algorithm for this problem? It might help you gather your thoughts (and write working code) to write that up first.
For eg, If I try to do that right now:
whatIsInAName([{ first: “Romeo”, last: “Montague” }, { first: “Mercutio”, last: null }, { first: “Tybalt”, last: “Capulet” }], { last: “Capulet” });
I can see that the last argument (source) can have multiple properties (at least 1, but can be more). And they all have to be matched exactly in order for the result to not be a blank array. (otherwise, just return a blank array)
I can see that the first argument (collection) is an array of objects that I can use to do the match.
So maybe , very roughly, something like this can work:
filter the collection so that {
all the key-value pairs in the source match the current collection item
}
So just by writing the above, I would immediately think, hmm, maybe try a filter command and inside it write a function that returns true only if the all the source key-value pairs match the current collection item.
Not sure if that helps guide you.
(again very rough thoughts, normally I would go and write the algorithm and then test it out and adjust as needed)