Wherefore Art Thou Bonfire Algorithm

Wherefore Art Thou Bonfire Algorithm
0

#1

I’m looking at Rafase282’s bonfire algorithm for Wherefore Art Though Challenge.

I noticed if I omit obj.hasOwnProperty(key) and just return obj[key] === source[key];, I still pass the challenge tests with the same results. I also ran the code through Cloud9 IDE and both versions of the code outputted the same results.

So is it really necessary to test for hasOwnProperty(key)?

function where(collection, source) {
  var arr = [];
  var keys = Object.keys(source);
  // Filter array and remove the ones that do not have the keys from source.
  arr = collection.filter(function(obj) {
    //Use the Array method every() instead of a for loop to check for every key from source.
    return keys.every(function(key) {
      // Check if the object has the property and the same value.
      return obj.hasOwnProperty(key) && obj[key] === source[key];
    });
  });

  return arr;
}

#3

Ok, so is hasOwnProperty checking to make sure the key exists or that the key has a value?


#5

Yes, but when it encounters a key value pair like { first: “Romeo”, last: “Montague” }, is it making sure “Romeo” and “Montague” exists for the keys (first and last) or is it just making sure the keys themselves (first and last) exist?


#7

Thanks for the explanation