-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhereforeArtThou.js
More file actions
82 lines (65 loc) · 2.27 KB
/
whereforeArtThou.js
File metadata and controls
82 lines (65 loc) · 2.27 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const log = console.log;
function whatIsInAName(collection, source) {
let sourceKeys = Object.keys(source); // ['last'] keys
return collection.filter(function(obj){
return sourceKeys.every(function(key){
return obj.hasOwnProperty(key) && obj[key] == source[key];
})
});
};
console.log(
whatIsInAName([{ lalapalooza: "Romeo", capital:"France" },
{ something: "Mercutio", last: "Capulet" },
{ first: "Tybalt", last: "Capulet" }],
{ last: "Capulet" }));
//////////////////////////////////////////////////////////////////////////////////////////////////////
function whatIsInAName2(collection, source) {
let sourceKeys = Object.keys(source);
console.log(sourceKeys); // ['last'] keys
return collection.filter(function (obj) {
for (let i=0;i<sourceKeys.length;i++){
if(!obj.hasOwnProperty(sourceKeys[i]) || obj[sourceKeys[i]] !== source[sourceKeys[i]]) {
return false;
}
}
return true;
});
}
console.log(
whatIsInAName2([{ lalapalooza: "Romeo", capital: "Montague" },
{ something: "Mercutio", another: "Capulet" },
{ first: "Tybalt", last: "Capulet" }],
{last: "Capulet" }))
/////////////////////////////////////////////////////////////////////////////////////////////////////
function whatIsInAName3(collection, source) {
let sourceKeys = Object.keys(source); // ['last'] keys
return collection.filter(function (obj) {
for(let key of sourceKeys){
if(!obj.hasOwnProperty(key) || obj[key] !== source[key]){
return false;
}
}
return true;
});
}
console.log(
whatIsInAName3([{ lalapalooza: "Romeo", capital: "Montague" },
{ something: "Mercutio", another: "Capulet" },
{ first: "Tybalt", last: "Capulet" }],
{last: "Capulet" }));
///////////////////////////////////////////////////////////////////////////////////////////////
function whatIsInAName4(collection, source) {
var srcKeys = Object.keys(source);
return collection.filter(function (obj) {
return srcKeys.map(function(key) {
return obj.hasOwnProperty(key) && obj[key] === source[key];
}).reduce(function(a, b) {
return a && b;
});
});
}
// test here
console.log(
whatIsInAName4([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null },
{ first: "Tybalt", last: "Capulet" }],
{ last: "Capulet" }));