Sometimes, we want to add the equivalent of Ruby Array.any? in JavaScript.
In this article, we’ll look at how to add the equivalent of Ruby Array.any? in JavaScript.
How to add the equivalent of Ruby Array.any? in JavaScript?
To add the equivalent of Ruby Array.any? in JavaScript, we can use the array some method.
For instance, we write
const isBiggerThan10 = (element, index, array) => {
return element > 10;
};
const hassBiggerThan10 = [2, 5, 8, 1, 4].some(isBiggerThan10);
to call [2, 5, 8, 1, 4].some with the isBiggerThan10 function to check if there’re any elements in [2, 5, 8, 1, 4] that’s bigger than 10 with isBiggerThan10.
It returns false since there’re no elements in [2, 5, 8, 1, 4] bigger than 10.
If there’re any elements bigger than 10, it’ll return true.
Conclusion
To add the equivalent of Ruby Array.any? in JavaScript, we can use the array some method.