Lodash _.some() 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 _.some() method is used to check if predicate return true for any element of collection. Iteration is stopped once predicate return true.
Syntax:
_.some(collection, predicate)
Parameters: This method accepts two parameters as mentioned above and described below:
- collection: This parameter holds the collection to iterate over.
- predicate: This parameter holds the function invoked per iteration and is invoked with three arguments (value, index|key, collection).
Return Value: This method is used to return true if any element passes 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 libraryconst _ = require("lodash"); // Original array and use of _.some() methodvar gfg = _.some([null, 0, 'yes', false], Boolean);// Printing the outputconsole.log(gfg); |
Output:
true
Example 2:
Javascript
// Requiring the lodash libraryconst _ = require("lodash"); // Original arrayvar object = [ { 'obj': 'moto', 'active': true }, { 'obj': 'lenovo', 'active': false } ]; // Use of _.some() method// The `_.matches` iteratee shorthandlet gfg = _.some(object, { 'obj': 'moto', 'active': false });// Printing the outputconsole.log(gfg); |
Output:
false
Example 3:
Javascript
// Requiring the lodash libraryconst _ = require("lodash"); // Original arrayvar object = [ { 'obj': 'moto', 'active': true }, { 'obj': 'lenovo', 'active': false } ]; // Use of _.some() method// The `_.matchesProperty` iteratee shorthandlet gfg = _.some(object, ['active', false]);// Printing the outputconsole.log(gfg); |
Output:
true
Example 4:
Javascript
// Requiring the lodash libraryconst _ = require("lodash"); // Original arrayvar object = [ { 'obj': 'moto', 'active': true }, { 'obj': 'lenovo', 'active': false } ]; // Use of _.some() method// The `_.property` iteratee shorthandlet gfg = _.some(object, 'active');// Printing the outputconsole.log(gfg); |
Output:
true
Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.


