Truth check collection

JavaScript, Object, Logic, Array · Oct 22, 2020

Checks if the predicate function is truthy for all elements of a collection.

  • Use Array.prototype.every() to check if each passed object has the specified property and if it returns a truthy value.
const truthCheckCollection = (collection, pre) =>
  collection.every(obj => obj[pre]);
truthCheckCollection(
  [
    { user: 'Tinky-Winky', sex: 'male' },
    { user: 'Dipsy', sex: 'male' },
  ],
  'sex'
); // true

More like this

  • Pick matching object keys

    Creates an object composed of the properties the given function returns truthy for.

    JavaScript, Object · Oct 22, 2020

  • Partition array in two

    Groups the elements into two arrays, depending on the provided function's truthiness for each element.

    JavaScript, Array · Nov 3, 2020

  • Map an object to an array

    Maps an object to an object array, using the provided mapping function.

    JavaScript, Object · Feb 5, 2023