-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectArrayLoop.js
More file actions
25 lines (20 loc) · 1.02 KB
/
ObjectArrayLoop.js
File metadata and controls
25 lines (20 loc) · 1.02 KB
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
function whatIsInAName(collection, source) {
//Declare variable that contains object info for search
var keyMatches = Object.keys(source);
// Filter through array collection
return collection.filter(function (obj) {
//Loop through collections to find a matching key
for(var i = 0; i < keyMatches.length; i++) {
//if the object being tested has the same property or is not equal to source then return false, else return true
if(!obj.hasOwnProperty(keyMatches[i]) || obj[keyMatches[i]] !== source[keyMatches[i]]) {
return false;
}
}
return true;
});
}
//Test samples
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
whatIsInAName([{ "a": 1 }, { "a": 1 }, { "a": 1, "b": 2 }], { "a": 1 });
whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 });
whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "c": 2 });