Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value:
4.
Examples 
4.1.
Combining Multiple Checks:
4.2.
JavaScript
4.3.
Validating Object Properties:
4.4.
JavaScript
4.5.
Using with Array Elements:
4.6.
JavaScript
4.7.
Filtering Based on Multiple Criteria:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.overSome() differ from _.overEvery()?
5.2.
Can _.overSome() handle asynchronous predicates?
5.3.
Is _.overSome() efficient for a large number of predicates?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.overSome() Method

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

In JavaScript and functional programming, it's common to encounter scenarios where you need to check if any one of multiple conditions is met. Lodash's _.overSome() method addresses this need by creating a function that returns true if any of the provided predicates return truthy for a given set of arguments. 

Lodash _.overSome() Method

This method is especially useful for validating data against multiple criteria, where satisfying any one condition is sufficient.

Why This Function is Used

The _.overSome() function is used to combine multiple predicate functions into a single function that checks if at least one predicate passes (returns truthy) for given arguments. This is crucial in scenarios where an action or decision depends on satisfying at least one of multiple conditions, enabling a more streamlined and flexible approach to validation or conditional logic.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax: 

_.overSome([predicates=[_.identity]])

Parameters:

[predicates=[_.identity]] (...(Function|Function[])): The functions to check.

Return Value:

(Function) - Returns the new composite function.

Examples 

Combining Multiple Checks:

  • JavaScript

JavaScript

var _ = require('lodash');

var isEven = n => n % 2 === 0;

var isNegative = n => n < 0;

var isEither = _.overSome([isEven, isNegative]);

console.log(isEither(3));

console.log(isEither(-3));

 Output:

 false (neither even nor negative)
 true (negative)


Demonstrates using _.overSome() to create a function that checks if a number is either even or negative.

Validating Object Properties:

  • JavaScript

JavaScript

var hasName = obj => !!obj.name;

var hasAge = obj => !!obj.age;

var isValidPerson = _.overSome([hasName, hasAge]);

console.log(isValidPerson({ name: 'John' }));

console.log(isValidPerson({ age: 30 }));

Output:

 true (has name)
 true (has age)


Shows how to validate an object based on the presence of certain properties.

Using with Array Elements:

  • JavaScript

JavaScript

var isString = val => typeof val === 'string';

var isNumber = val => typeof val === 'number';

var checkElements = _.overSome([isString, isNumber]);

var mixedArray = [1, 'a', true, {}, []];

var hasValidElements = mixedArray.some(checkElements);

console.log(hasValidElements);

Output:

 true (contains strings or numbers)


An example of using _.overSome() to check if an array contains specific types of elements.

Filtering Based on Multiple Criteria:

  • JavaScript

JavaScript

var products = [

 { name: 'Apple', category: 'fruit', stock: 0 },

 { name: 'Carrot', category: 'vegetable', stock: 10 }

];

var isFruit = product => product.category === 'fruit';

var isInStock = product => product.stock > 0;

var desirableProducts = _.filter(products, _.overSome([isFruit, isInStock]));

console.log(desirableProducts);

Output:

 [{ name: 'Apple', category: 'fruit', stock: 0 }, { name: 'Carrot', category: 'vegetable', stock: 10 }]


Demonstrates filtering a collection based on multiple conditions using _.overSome().

Frequently Asked Questions

How does _.overSome() differ from _.overEvery()?

_.overSome() checks if any of the predicates return truthy (like a logical OR), whereas _.overEvery() checks if all predicates return truthy (like a logical AND).

Can _.overSome() handle asynchronous predicates?

_.overSome() is designed for synchronous predicate functions. Asynchronous predicates would require handling with Promises or async/await.

Is _.overSome() efficient for a large number of predicates?

The efficiency of _.overSome() depends on the complexity and performance of the individual predicates. For a large number of computationally intensive predicates, performance considerations may be necessary.

Conclusion

Lodash's _.overSome() method is a useful tool for combining multiple predicate functions into a single function that checks if at least one condition is met. It simplifies validation logic and is particularly beneficial in scenarios requiring flexible and inclusive conditional checks.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Lodash _.lowerFirst() Method
Next article
Lodash _.bindAll() Method
Live masterclass