Wherefore art thou using map and filter

Wherefore art thou using map and filter
0

#1

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.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/


#2

the only remaining conclusion is that ‘arr’ is not an array.

Try adding the missing semicolon on the return line also to see if that helps.


#3

I console logged it and it came up as an object. so arr is really 2 obj. is that correct?
image


#4

in this case sadly it is an object but not an array:


#5

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?


#6

If you are trying to access every property of the object just grab an array of the all the properties to apply map to. for eg. like this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys


#7

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;
}

whatIsInAName([{ first: “Romeo”, last: “Montague” }, { first: “Mercutio”, last: null }, { first: “Tybalt”, last: “Capulet” }], { last: “Capulet” });


#8

btw, I’m not 100% sure yet what you are trying to do with the loop so it might help to explain that a bit.
but anyway, it looks like you actually want to check if a property exists?
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)
hasOwnProperty can be used for that.

To paste a repl.it, I just paste the url of the repl.it

Let me try your new code to try to understand what you are saying in the meantime…


#9

ok I understand what you meant now.
You can’t ask an object if it contains another object with the hasOwnProperty function

That is, in your code above you said:
if(collection[i].hasOwnProperty(source))

But ‘source’ is also an object and not a property.


#10

Sorry I didn’t explain it thoroughly. I see know.


#11

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)