Files
30-seconds-of-code/snippets/Truth-check-objects.md
2017-12-16 18:21:32 +05:30

19 lines
724 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### Truth-Check-Objects
Check if the predicate (second argument) is truthy on all elements of a collection (first argument).
- For every object in the collection array, check the truthiness of objects property passed in pre parameter
- Array#every method internally checks if the value returned from the callback is truthy.
- Return true if it passes for every object. Otherwise, return false.
- Also if the object is empty then it will return false
```js
truthCheck = (collection, pre) => (collection.every(obj => obj[pre]));
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"},
{"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
// true
```