-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwherefore-art-thou.js
More file actions
37 lines (31 loc) · 920 Bytes
/
wherefore-art-thou.js
File metadata and controls
37 lines (31 loc) · 920 Bytes
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
26
27
28
29
30
31
32
33
34
35
36
37
/*
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.
*/
function whatIsInAName(collection, source) {
var arr = [];
var sourceKeys = Object.keys(source);
for (var person in collection) {
var haveAll = true;
for (var key in sourceKeys) {
if (collection[person][sourceKeys[key]] !== source[sourceKeys[key]]) {
haveAll = false;
break;
}
}
if (haveAll) arr.push(collection[person]);
}
return arr;
}
console.log(
whatIsInAName(
[
{ first: "Romeo", last: "Montague" },
{ first: "Mercutio", last: null },
{ first: "Tybalt", last: "Capulet" }
],
{ last: "Capulet" }
)
);