every() Function

In Google Apps Script, the every() method is an array method that tests whether all elements in an array pass a specified test. It takes a callback function as an argument, which is called for each element in the array. The callback function should return a boolean value: true if the element passes the test, and false if it does not.

Here is an example of using the every() method to test whether all numbers in an array are greater than 5:

var numbers = [6, 7, 8, 9, 10];

// Test whether all numbers in the array are greater than 5
var allGreater = numbers.every(function(num) {
  return num > 5;
});

// Output the result
Logger.log(allGreater); // Outputs: true

In this example, we call the every() method on the numbers array and pass a callback function that tests whether each number is greater than 5. Since all numbers in the array pass the test, the every() method returns true. The result is assigned to a variable called allGreater, which is outputted to the log using the Logger.log() method.

You can also use the every() method to test whether all objects in an array meet a certain criteria:

var persons = [
  { name: "John", age: 30 },
  { name: "Jane", age: 25 },
  { name: "Bob", age: 40 }
];

// Test whether all persons in the array are over 18 years old
var allAdults = persons.every(function(person) {
  return person.age > 18;
});

// Output the result
Logger.log(allAdults); // Outputs: true

In this example, we call the every() method on the persons array and pass a callback function that tests whether each person’s age is greater than 18. Since all persons in the array meet this criterion, the every() method returns true. The result is assigned to a variable called allAdults, which is outputted to the log using the Logger.log() method.