forked from kallaway/100-days-of-code
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfindValueInObject.js
36 lines (32 loc) · 1.17 KB
/
findValueInObject.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
26
27
28
29
30
31
32
33
34
35
36
// https://www.freecodecamp.com/challenges/wherefore-art-thou
/*
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching property and value pairs (second argument). Each property 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.
The source object is only an object, with one or more properties.
The collection is an array of several objects.
*/
function whatIsInAName(collection, source) {
var arr = [];
var match;
for (var i = 0; i < collection.length; i++) {
for (var key in source) {
if ( collection[i].hasOwnProperty(key) ) {
console.log(key);
if ( collection[i][key] == source[key] ) {
match = true;
continue;
} else {
match = false;
break;
}
} else {
match = false;
break;
}
}
if (match) {
arr.push( collection[i] );
}
}
return arr;
}
whatIsInAName( [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" } );