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.

Syntax

array.some(callbackFunction)

Parameters

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 return false.

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 the some() 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.

Examples

Example 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

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

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.

Stay up to date

Follow me via email to receive actionable tips and other exclusive content. I'll also send you notifications when I publish new content.
By signing up you agree to the Privacy Policy & Terms.

Have feedback for me?

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!