JavaScript Array Some; In this tutorial, you will learn JavaScript Array some()
method and how to use this method.
JavaScript Array some()
method
When you work with javascript arrays and want to check if an array has at least one element that satisfies a specified condition.
See the following array in js:
let numbers = [ 1, 3, 5, 7, 10, 12, 15, 18 ];
Basically, you use a for loop and if statement for test a specified condition, like this:
let numbers = [ 1, 3, 5, 7, 10, 12, 15, 18 ]; let result = false; for (let index = 0; index < numbers.length; index++) { if (numbers[index] % 3 === 0 && numbers[index] % 5 === 0) { result = true; break; } } console.log(result);
Output:
true
How it works:
- First of all, declare numeric array.
- Next, declare a flag variable
result
and assign value tofalse
. - Iterate for loop over each element. And test specified conditions using if statement. If the condition met true, then assign the result to
true
and after that exit the loop using thebreak
statement. - Finally, print the result.
Instead of js for loop and if statement, you can use the some()
method, test at least one element with the specified condition.
See the following example:
let numbers = [ 1, 3, 5, 7, 10, 12, 15, 18 ]; let result = numbers.some(function(e) { return e % 3 === 0 && e % 5 === 0; }); console.log(result);
Output
true
The condition is implemented via a js callback function passed in some () method.
Now, the js script is more concise and more readable way. So, you can use the arrow function syntax in ES6:
let numbers = [ 1, 3, 5, 7, 10, 12, 15, 18 ]; let result = numbers.some(e => e % 3 === 0 && e % 5 === 0); console.log(result);
Syntax JavaScript Array some()
The following represents the syntax of the some()
method:
arrayObject.some(callback[, thisArg]);
The some()
method accepts two parameters:
1) The callback
parameters
The some()
function executes the callback
function once for each element in the array continuously it finds the one where the callback
function returns a true
. The some()
method immediately returns true
and doesn’t decide the remaining elements.
If no element causes the callback()
to return true
, the some()
method returns false
.
The callback
function accepts 3 parameters:
function callback(currentElement [[, currentIndex], array]){ // ...}
- The
currentElement
is the current element of an array. - The
currentIndex
is the index of the current element. - The
array
is array thatsome()
was called upon.
2) The thisArg
parameter
The thisArg
parameter is optional. If you pass the thisArg
into the method, you can use the thisArg
as the this
value inside the callback
function.
Example: JavaScript Array some()
Let’s look at more examples of using the javascript some()
method.
1) Check if an element exists in the array
Using theisExists()
function to test if a passed element/value exists in an array, this function uses the some()
method.
You can see the following:
function isExists(value, array) { return array.some(e => e === value); } let numbers = [ 1, 3, 5, 7, 10, 12, 15, 18 ]; console.log(isExists(5, numbers)); console.log(isExists(15, numbers));
Output:
true false
2) Check if an array has one element that is in a range
Check if a given number between provided range in an array.
See the following example:
let marks = [4, 5, 7, 9, 10, 2]; const range = { min: 8, max: 10 }; let result = marks.some(function (e) { return e >= this.min && e <= this.max; }, range); console.log(result);
Output:
true
Note that, If you call the javascript some()
method on an empty array, the result is always false
regardless of any condition.
See the following example:
let result = [].some(e => e > 0); console.log(result);
Output:
false
Conclusion
In this tutorial, you have learned how to use the JavaScript Array some() method to test if an array has at least one element that meets a condition.