Underscore.js _.every Function
_.every(list, [predicate], [context])
Parameters: This function accepts three parameters as mentioned above and described below:
- List: This parameter is used to set the list of elements.
- Predicate: This parameter is used to test the condition.
- Context: This parameter is used to display the content.
Return values: The returned value which is either ‘true’ ( when every element of the list fulfills the given condition) or ‘false’ ( when at least one element does not fulfill the condition) Passing an array to the _every function(): The ._every() function takes the element from the list one by one and do the specified operations on the code. Below example contains the operation to find all the elements of the list that are valid or not. Valid means that they do not contain Null, Blanks, false etc. After traversing and checking all the elements, the every function ends. Here even if a single element is not valid then also the answer is false.
Output: Passing a list of numbers to _.every() function: Pass a list of numbers and do the simple operations on it. Below example is used to find whether a number is even or not. If all the numbers in the list are even then the output is true otherwise false. Example:
Output: Passing a structure to the _.every() function: First declare the array (The name of array is people). Choose one condition to check hasLongHairs. Console.log display the final answer. Example:
Output: Using two _.every() function together: Pass different objects to each _.every() function and then use the following results together by using the logical operators like &&, ||, ! etc. Here, the object1 and arralist1 contains all the true values so the resultant of two true will also be true. Hence, first condition is satisfied. The object2 contains ‘null’ and arraylist2 also contains ‘null’ so they are not valid. Use ‘!’ before every _.every() function so the resultant are two true values. Example: