Lodash _.some() Method
Last Updated :
18 Oct, 2023
Lodash _.some() method is used to check if the predicate returns true for any element of the collection. Iteration is stopped once the predicate returns true.
Syntax:
_.some(collection, [predicate])
Parameters:
- collection (Array|Object) parameter holds the collection to iterate over.
- predicate(Function) 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: In this example, It is returning true as it has passed the predicate check.
Javascript
const _ = require( "lodash" );
let gfg = _.some([ null , 0, 'yes' , false ], Boolean);
console.log(gfg);
|
Output:
true
Example 2: In this example, It is returning false as it has not passed the predicate check.
Javascript
const _ = require( "lodash" );
let object = [
{ 'obj' : 'moto' , 'active' : true },
{ 'obj' : 'lenovo' , 'active' : false }];
let gfg = _.some(object, { 'obj' : 'moto' , 'active' : false });
console.log(gfg);
|
Output:
false
Example 3: In this example, It is returning true as it has passed the predicate check.
Javascript
const _ = require( "lodash" );
let object = [
{ 'obj' : 'moto' , 'active' : true },
{ 'obj' : 'lenovo' , 'active' : false }];
let gfg = _.some(object, [ 'active' , false ]);
console.log(gfg);
|
Output:
true
Example 4: In this example, It is returning true as it has passed the predicate check.
Javascript
const _ = require( "lodash" );
let object = [
{ 'obj' : 'moto' , 'active' : true },
{ 'obj' : 'lenovo' , 'active' : false }];
let gfg = _.some(object, 'active' );
console.log(gfg);
|
Output:
true
Share your thoughts in the comments
Please Login to comment...