-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhereforeArtThou.js
25 lines (20 loc) · 1.16 KB
/
WhereforeArtThou.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Make a function that looks through an array of objects (first argument) and returns an array
// of all objects that have matching name and value pairs (second argument). Each name and value
// pair of the source object has to be present in the object from the collection if it is to be
// included in the returned array.
// For example, if the first argument is [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null },
// { first: "Tybalt", last: "Capulet" }], and the second argument is { last: "Capulet" },
// then you must return the third object from the array (the first argument), because
// it contains the name and its value, that was passed on as the second argument.
function whatIsInAName(collection,source){
const sourceKeys = Object.keys(source);
return collection.filter(obj => {
for(let i = 0; i < sourceKeys.length; i++){
if(obj[sourceKeys[i]] !== source[sourceKeys[i]]){
return false;
}
}
return true;
})
}
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));