-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhatIsInAName.js
More file actions
25 lines (18 loc) · 969 Bytes
/
whatIsInAName.js
File metadata and controls
25 lines (18 loc) · 969 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
function whatIsInAName(collection, source) {
// get the key value(s) of the source object
// Object.keys() returns an array whose elements are strings corresponding
// to the enumerable keys found in an object.
var sourceKeys = Object.keys(source);
// The hasOwnProperty() method returns a boolean indicating whether
// the object has the specified property
return collection.filter(function(obj) {
// for every key from sourceKeys array
return sourceKeys.every(function(key) {
// if the object from the collection array has the key(s) stored in
// sourceKeys array and if the object keys are the same as the source keys,
// filter only that objects that satisfy this criteria
return obj.hasOwnProperty(key) && obj[key] === source[key];
});
});
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });