Problem Explanation
Write an algorithm that will take an array for the first argument and return an array with all the objects that matches all the properties and values in the Object passed as second parameter.
Hint: 1
You may use for loop or the Array.prototype.filter method.
Hint: 2
Try to use Object.prototype.hasOwnProperty method to know if the property name exists in an object (as its own property).
Hint: 3
Check equivalence of Object in collection with Object passed as second parameter to where function.
Spoiler Alert!
Solution ahead!
Code Solution:
First
function where(collection, source) {
// "What's in a name? that which we call a rose
// By any other name would smell as sweet.”
// -- by William Shakespeare, Romeo and Juliet
var srcKeys = Object.keys(source);
// filter the collection
return collection.filter(function (obj) {
for(var i = 0; i < srcKeys.length; i++) {
// check if obj in collection doesn't have the key
// or if it does have the key,
// then check if the property value doesn't match the value in source
if(!obj.hasOwnProperty(srcKeys[i]) || obj[srcKeys[i]] !== source[srcKeys[i]]) {
return false;
}
}
return true;
});
}Second
function where(collection, source) {
// "What's in a name? that which we call a rose
// By any other name would smell as sweet.”
// -- by William Shakespeare, Romeo and Juliet
var srcKeys = Object.keys(source);
// filter the collection
return collection.filter(function (obj) {
// return a Boolean value for `filter` method
return srcKeys.every(function (key) {
// reduce to Boolean value to be returned for `every` method
return obj.hasOwnProperty(key) && obj[key] === source[key];
});
});
}Third
function where(collection, source) {
// "What's in a name? that which we call a rose
// By any other name would smell as sweet.”
// -- by William Shakespeare, Romeo and Juliet
var srcKeys = Object.keys(source);
// filter the collection
return collection.filter(function (obj) {
// return a Boolean value for filter callback using reduce method
return srcKeys.reduce(function (res, key) {
// reduce to Boolean value to be returned by reduce method
return obj.hasOwnProperty(key) && obj[key] === source[key];
}, false);
});
}Fourth
function where(collection, source) {
//Array for storing the matched objects
var arr = [];
//For maintaining the number of key-value matches
var count ;
collection.forEach(function(collObj) {
//Initialize to zero for each object in collection
count = 0 ;
for(var prop in source){
//Check whether the key exists and the key-value is matched
if( collObj.hasOwnProperty(prop) && collObj[prop] === source[prop])
//Increase the count if matched
count++;
}
//Check whether all key-value pairs in source are matched
if(count>=Object.keys(source).length){
arr.push(collObj);
}
});
return arr;
}Code Explanation:
Please read the comments in the source code.
Check this http://repl.it/BFZQ/19 for pretty debugging and code explanation
Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @abhisekp @dting @coded9 for your help with Algorithm: Where art Thou
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)
