Syntax, Parameter and Return Value
Syntax:
_.every(collection, [predicate=_.identity])
Parameters:
-
collection (Array|Object): The collection to iterate over.
- [predicate=_.identity] (Function): The function invoked per iteration.
Return Value:
(boolean) - Returns true if all elements pass the predicate check, else false.
Examples
Checking All Numbers for Evenness:
JavaScript
var _ = require('lodash');
var numbers = [2, 4, 6, 8, 10];
var allEven = _.every(numbers, n => n % 2 === 0);
console.log(allEven);
You can also try this code with Online Javascript Compiler
Run Code
Output:
true
This example demonstrates checking if all numbers in an array are even.
Validating Object Properties:
JavaScript
var users = [{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': false }];
var allInactive = _.every(users, {'active': false});
console.log(allInactive);
You can also try this code with Online Javascript Compiler
Run Code
Output:
true
Here, _.every() is used to verify if all objects in an array have a specific property value.
Array of Strings:
JavaScript
var words = ['apple', 'amber', 'arc'];
var allStartWithA = _.every(words, w => w.startsWith('a'));
console.log(allStartWithA);
You can also try this code with Online Javascript Compiler
Run Code
Output:
true
Checks if every string in an array starts with the letter 'a'.
Using Function Shorthand:
JavaScript
var objects = [{ 'n': 4 }, { 'n': 6 }, { 'n': 8 }];
var allGreaterThanThree = _.every(objects, 'n', n => n > 3);
console.log(allGreaterThanThree);
You can also try this code with Online Javascript Compiler
Run Code
Output:
true
Utilizing Lodash's shorthand syntax for cleaner and more concise code.
Frequently Asked Questions
How does _.every() behave with an empty collection?
_.every() returns true for empty collections as there are no elements to fail the predicate test, adhering to the principle of vacuous truth.
Can _.every() work with collections containing different data types?
Yes, but the predicate function must be capable of handling the various data types present in the collection.
Is _.every() efficient for large datasets?
_.every() is efficient for most datasets, but its performance depends on the complexity of the predicate function and the size of the dataset.
Conclusion
Lodash's _.every() method is a concise and effective tool for verifying that all elements in a collection meet a specific criterion. It simplifies the process of uniform condition checking, enhancing both the readability and the reliability of code. This method is an excellent example of how Lodash facilitates more expressive and efficient JavaScript programming.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.