The Wayback Machine - https://web.archive.org/web/20220505183934/https://www.geeksforgeeks.org/lodash-_-some-method/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Lodash _.some() Method

  • Difficulty Level : Basic
  • Last Updated : 27 Sep, 2021

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 library
const _ = require("lodash");
      
// Original array and use of _.some() method
var gfg = _.some([null, 0, 'yes', false], Boolean);
 
// Printing the output
console.log(gfg);

 
Output:  

true

Example 2: 

Javascript




// Requiring the lodash library
const _ = require("lodash");
      
// Original array
var object = [
  { 'obj': 'moto', 'active': true },
  { 'obj': 'lenovo',   'active': false } ];
  
// Use of _.some() method
// The `_.matches` iteratee shorthand
 
let gfg = _.some(object, { 'obj': 'moto', 'active': false });
 
// Printing the output
console.log(gfg);

 
Output:  

false

Example 3: 

Javascript




// Requiring the lodash library
const _ = require("lodash");
      
// Original array
 
var object = [
  { 'obj': 'moto', 'active': true },
  { 'obj': 'lenovo',   'active': false } ];
  
// Use of _.some() method
// The `_.matchesProperty` iteratee shorthand
 
let gfg = _.some(object, ['active', false]);
 
// Printing the output
console.log(gfg);

 
Output:  

true

Example 4: 

Javascript




// Requiring the lodash library
const _ = require("lodash");
      
// Original array
var object = [
  { 'obj': 'moto', 'active': true },
  { 'obj': 'lenovo',   'active': false } ];
  
// Use of _.some() method
// The `_.property` iteratee shorthand
 
let gfg = _.some(object, 'active');
 
// Printing the output
console.log(gfg);

 
Output:  

true

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!