Skip to content

Instantly share code, notes, and snippets.

@jepras
Created October 5, 2018 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepras/53824217b755bc9684374ae3e9905515 to your computer and use it in GitHub Desktop.
Save jepras/53824217b755bc9684374ae3e9905515 to your computer and use it in GitHub Desktop.
Used hasOwnProperty to check collection objects had the second argument in them to increase a counter. Finally check counter vs length to check if all values were true.
function truthCheck(collection, pre) {
// Is everyone being true?
var counter = 0;
for (let c in collection) {
if (collection[c].hasOwnProperty(pre) && Boolean(collection[c][pre])) {
counter++;
}
}
if (counter == collection.length) {
return true;
}
return false;
}
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment