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