Why doesn't this work? (filter question) [SOLVED]

Why doesn't this work? (filter question) [SOLVED]


I’m doing the Wherefore Art Thou challenge and I’m trying to understand why this code won’t work.
I’m trying to get the object from the 2nd argument, and match it against the array of objects in the first argument, so I save the keys,
var sourceKeys = Object.keys(source);

and then filter through the object array with this function.

filtered = collection.filter(function (obj){
    for (i=0; i<sourceKeys.length; i++){
      if (obj.hasOwnProperty(sourceKeys[i]) && obj.sourceKeys[i] == source.sourceKeys[i]) { 
        return true;
  return false;                         

My thinking is, with the second condition of the if statement obj.sourceKeys[i] == source.sourceKeys[i], I can take the key and access the property in each array with it, but i get an error that says obj.sourceKeys is undefined. Since it is an array, why can’t i just plug in the key?


So I tried it with bracket notation instead and it works now…
obj[sourceKeys[i]] == source[sourceKeys[i]] Should’ve tried that first