The every
function from the Underscore library allows you to test whether every element in an array passes a particular condition. Here’s how you can use it:
First, you need to include the Underscore library in your project. You can do this by adding the following line of code to your HTML file:
38 chars2 lines
Next, you can use the every
function to test whether every element in an array passes a particular condition. Here’s the basic syntax:
index.tsx38 chars2 lines
list
: This is the array you want to test.predicate
: This is the function that tests each element in the array.context
: This is the optional context that the predicate
function will be called with.The predicate
function should return a boolean value that indicates whether the element passes the test. If every element in the array passes the test, the every
function returns true
. Otherwise, it returns false
.
Here’s an example that uses the every
function to test whether every element in an array is greater than 0:
index.tsx136 chars6 lines
In this example, the _.every()
function returns true
because every element in the numbers
array is greater than 0.
Note that you can also use the every
function with an object instead of an array. In this case, the predicate
function will be called with each value in the object.
gistlibby LogSnag