Lodash _.every() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers, etc. The _.every method checks if the predicate returns true for all elements of collection and iteration is stopped once the predicate returns falsely.
Note: Also this method returns true for empty collections because everything is true for elements of empty collections.
Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!
Syntax:
_.every(collection, [predicate=_.identity])
Parameters: This method accepts two parameters as mentioned above and described below:
- collection (Array|Object): This parameter holds the collection to iterate over.
- [predicate=_.identity] (Function): This parameter holds the function invoked per iteration.
Return Value: This method is used to returns true if all elements pass the predicate check, else false.
Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.
javascript
// Requiring the lodash library const _ = require("lodash"); // Original array var obj1 = ([true, 1, null, 'yes']);var obj2 = ([true, 2, 'active', 'yes']); // Use of _.every() method let x = _.every(obj1, Boolean); let y = _.every(obj2, Boolean); // Printing the output console.log(x);console.log(y); |
Output:
false true
Example 2:
javascript
// Requiring the lodash library const _ = require("lodash"); // Original array var users = [ { 'user': 'jonny', 'age': 30, 'active': false }, { 'user': 'harry', 'age': 35, 'active': false } ]; // Use of _.every() method// The `_.matches` iteratee shorthand. let x = _.every(users, { 'user': 'barney', 'active': false }); // Printing the output console.log(x); |
Output:
false
Example 3:
javascript
// Requiring the lodash library const _ = require("lodash"); // Original array var users = [ { 'user': 'jonny', 'age': 30, 'active': false }, { 'user': 'harry', 'age': 35, 'active': false }]; // Use of _.every() method// The `_.matchesProperty` iteratee shorthand. let x = _.every(users, ['active', false]); // Printing the output console.log(x); |
Output:
true
Example 4:
javascript
// Requiring the lodash library const _ = require("lodash"); // Original array var users = [ { 'user': 'jonny', 'age': 30, 'active': false }, { 'user': 'harry', 'age': 35, 'active': false }]; // Use of _.every() method// The `_.property` iteratee shorthand. let x = _.every(users, 'active'); // Printing the output console.log(x); |
Output:
false
Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.


