The _.every() function is used to test all the elements of the list can pass the given test. It stops and returns ‘false’ if at least one element is not fulfill the given test. When all the elements of the list are passed to the function/iteratee and no more elements remains then the _.every function to traverse and the value false has not yet returned as answer then return true as final answer. Pass the numbers, characters, array, objects etc to the _.every function. Also, one can use to _.every() function together like inside an if loop etc.
_.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.
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.
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.
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 conatins ‘null’ so they are not valid. Use ‘!’ before every _.every() function so the resultant are two true values.