The Array method some() in Apps Script
The Array method some()
is used to check if at least one element in an array meets a set of conditions.
Prerequisites
This tutorial assumes that you're familiar with:
Syntaxarray.some(callbackFunction)
Parameters
array.some(callbackFunction)
callbackFunction (required):
This is the function that will be executed once for every element in the array. This function is where you need to specify the conditions that need to be checked. If all the conditions are met, this function should return
true
. If one or more conditions are not met, it should returnfalse
.
The callbackFunction
can accept three arguments:
value
(required): The current value being processed.index
(optional): The index of the value in the array.array
(optional): The original array that thesome()
method was called on.
Return value
The some()
method returns true
if at least one element in the array meets the conditions specified in the callback function. It returns false
if none of the elements in the array meet the conditions set forth in the callbackFunction
.
How does the some() Array method work?
The some()
method will execute the callbackFunction
once for every value in the array. If the callbackFunction
returns true
(or a value that is treated as true by Apps Script), the some()
method will immediately return true
. If the callbackFunction
returns false
(or a value that is treated as false
by Apps Script) for every value in the array, the some()
method will return false
.
Note: The following values in Apps Script are treated as false
:
false
: The boolean value false0
: The number 0""
: An empty stringnull
: The null valueundefined
: An undefined valueNaN
: A value that represents "not a number"
Other values are treated as true
by Apps Script. Interestingly, even empty arrays and empty objects are treated as true
by Apps Script.
ExamplesExample 1: Using the some() Array method to check if at least one element in an array is a number
Example 1.1
In the code below, the function isNumber(value)
is the callbackFunction parameter in the some()
method. This function is executed once for every element in the array values
and it returns true
if the element is a number and false
otherwise. Since at least one element in the array values
is a number, the some()
method returns true.
function someEx1_1() {
var values = ["apple", -3.4, 27.1, "purple", 0];
Logger.log(values.some(isNumber));
}
function isNumber(value) {
return typeof value === 'number'
}
Output
true
How does the above code work?
The some()
method will execute the callback function once for every element in the array values
. If the callback function returns true
, the some()
method will immediately return true
. If the callback function returns false
for every element, the some()
method will return false
.
Therefore, the function isNumber()
will be called 2 times. The first value "apple"
is not a number so the isNumber()
method will return false
. The second value -3.4
is a number and therefore isNumber()
will return true
and the some()
method will also return true
.
isNumber("apple"); //returns false
isNumber(-3.4); //returns true
Since the isNumber()
function returns true
for the value -3.4
, the some()
method returns true
.
Example 1.2
Instead of defining the function isNumber()
separately and then passing it as a parameter, you can also define it in-line. The code below shows you how to do this.
function someEx1_2() {
var values = ["apple", -3.4, 27.1, "purple", 0];
var output = values.some(function(value) {
return typeof value === 'number'
});
Logger.log(output);
}
Output
true
Note
Notice that there is no name for the function in the above example.
This is because, in Apps Script (which is a version of JavaScript), a function is basically a variable with a value of type function.
Consider the function addTwoNumbers()
below.
function addTwoNumbers(num1, num2) {
return num1 + num2;
}
This function can also be rewritten in the following way:
var addTwoNumbers = function(num1, num2) {
return num1 + num2;
}
Here the variable addTwoNumbers
contains a value of type function. In Apps Script, both of the above code snippets have the exact same result.
When you pass a function as a parameter to the some()
method, you're really passing the function value. Therefore, the following statement values.some(isNumber);
in Example 1.1 is the same as the code snippet below in Example 1.2
values.some(function(value) {
return typeof value === 'number'
});
This is because isNumber
is just a variable that contains the following function value:
function(value) {
return typeof value === 'number'
}
Example 2: The some() function will immediately return true when the callBack() function returns true the first time
The some()
array method will execute the callback function once for every value in the array until it encounters a value for which the callback function returns true
. As soon as the some()
method encounters a value that meets the conditions set forth in the callback function, the some()
method will return true and won't process the remaining elements in the array.
In the code below, the isNumberLog()
function will return true
for the value 1
since it is a number. The some()
method will return true
after it processes the value 1
and the remaining values 2, "3", 4, 5 and 6 will not be processed.
function someEx2() {
var values = [1, 2, "3", 4, 5, 6];
Logger.log(values.some(isNumberLog));
}
function isNumberLog(value) {
Logger.log(value);
return typeof value === 'number'
}
Output
1.0
true
Example 3: Empty arrays and objects are treated as "true" values in Apps Script
In the code below, the array values contains only empty arrays and objects and the callback function simply returns the value passed to it. Since Apps Script treats empty arrays and objects as a true
value, the callback function will return true for the very first element itself and therefore the some()
array will also return true. This value is then logged.
function someEx3() {
var values = [[], [], {}, {}];
Logger.log(values.some(function(value) {
return value;
}));
}
Output
true
Conclusion
In this tutorial, you learned how to use the some()
Array method to check if at least one element in the array meets a set of conditions.
Master Google Sheets Automation
I'd appreciate any feedback you can give me regarding this post.
Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!