-
Notifications
You must be signed in to change notification settings - Fork 2
Algorithm Wherefore Art Thou
🚩 Remember to use Read-Search-Ask
if you get stuck. Try to pair program 👥 and write your own code 📝
Write an algorithm that will take an array
for the first argument and return an array
with all the object
s that matches all the properties and values in the Object
passed as second parameter.
You may use for
loop or the Array.prototype.filter
method.
try to solve the problem now
Try to use the Object.prototype.hasOwnProperty
method to know if the property name exists in an object (as its own property).
try to solve the problem now
Check equivalence of Object
in collection
with Object
passed as second parameter to whatIsInAName
function.
try to solve the problem now
Solution ahead!
function whatIsInAName(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++) {
if(!obj.hasOwnProperty(srcKeys[i]) || obj[srcKeys[i]] !== source[srcKeys[i]]) {
return false;
}
}
return true;
});
}
// test here
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
🚀 Run Code
- We filter through the array using
.filter()
. - Using a
for loop
we loop through each item in the object. - We use a
if statement
to check if the object in the collection doesn't have the key and the property value doesn't match the value in source. - We return
false
if the aboveif statement
is correct. Otherwise, we returntrue
;
function whatIsInAName(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);
return collection.filter(function (obj) {
return srcKeys.every(function (key) {
return obj.hasOwnProperty(key) && obj[key] === source[key];
});
});
}
// test here
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
🚀 Run Code
- We filter through the collection using
.filter()
. - Next, we return a
Boolean
value for the.filter()
method. - Finally, we reduce to
Boolean
value to be returned for the.every()
method.
function whatIsInAName(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 srcKeys.reduce(function (res, key) {
return obj.hasOwnProperty(key) && obj[key] === source[key];
}, false);
});
}
// test here
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
🚀 Run Code
- We start by filtering through
collection
usingArray.filter()
. - Next, we return a
Boolean
value for the filter callback using.reduce()
. - We reduce to
Boolean
value to be returned by.reduce()
.
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: Wherefore Art Thou
⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See 👉
Wiki Challenge Solution Template
for reference.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links