-
Notifications
You must be signed in to change notification settings - Fork 1
/
everyThingBeTrue.js
18 lines (13 loc) · 1.1 KB
/
everyThingBeTrue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
Check if the predicate (second argument) is truthy on all elements of a collection (first argument)
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex") // true.
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex") should return false.
truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age") //false
truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastFoward", "onBoat": null}], "onBoat") //true
truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastFoward", "onBoat": true}], "onBoat") // true
*/
function truthCheck(collection, pre) {
return collection.every(function(item) {
return item.hasOwnProperty(pre) && Boolean(item[pre]);
});
}