how to use the every function from lodash in javascript

The every function from lodash tests whether all elements in an array pass a given condition. Here is how to use it:

index.tsx
const _ = require('lodash');

const array = [1, 2, 3, 4, 5];

const allGreaterThanZero = _.every(array, n => n > 0);
console.log(allGreaterThanZero); // Output: true

const allEven = _.every(array, n => n % 2 === 0);
console.log(allEven); // Output: false
256 chars
10 lines

In the code above, we load lodash and create an array to test. Then we pass each value of the array and a test function to _.every. The test function returns true or false depending on whether the value passes the test. Finally, _.every returns true if all values pass the test, and false otherwise.

gistlibby LogSnag