Algorithm Wherefore Art Thou - ashish9342/FreeCodeCamp GitHub Wiki
🚩 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.