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
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));
You can also try this code with Online Javascript Compiler
Run Code
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
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 }));
You can also try this code with Online Javascript Compiler
Run Code
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
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);
You can also try this code with Online Javascript Compiler
Run Code
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
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);
You can also try this code with Online Javascript Compiler
Run Code
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.