Apps Script Course: Advanced Arrays

We have previously seen how to create an array, access a value in an array and modify it, add a value to an array, know the number of values in an array, etc.

Now we will go much further with arrays, so if all this is not clear to you, start by taking a few minutes to go back and read the page on arrays.


Deleting a Value

You already know the push method which allows adding a value at the end of an array and unshift which allows adding a value at the beginning.

There are also two methods for removing a value at the beginning and end: pop which deletes the last value, and shift which deletes the first value.

Here is an example:

function test() {

  const array = [10, 20, 30, 40, 50];

  // Deleting the last value
  array.pop();
  console.log(array); // Displays: [ 10, 20, 30, 40 ]

  // Deleting the first value
  array.shift();
  console.log(array); // Displays: [ 20, 30, 40 ]
}

These two methods return the deleted value, so you can recover it when useful:

function test() {

  const array = [10, 20, 30, 40, 50];

  // Last value
  const lastValue = array.pop();
  console.log(lastValue); // Displays: 50

  // First value
  const firstValue = array.shift();
  console.log(firstValue); // Displays: 10
}

To delete one or more values starting from a defined position, use the splice method:

function test() {

  const array = [10, 20, 30, 40, 50];

  // Deleting 2 values starting at index 1
  const deletedValues = array.splice(1, 2);
  
  console.log(deletedValues); // Displays: [ 20, 30 ]
  console.log(array); // Displays: [ 10, 40, 50 ]
}

Position

To know the position of a value in an array, use the indexOf method:

function test() {

  const array = [10, 20, 30, 40, 50];

  // Position of the number 40
  const position = array.indexOf(40);
  console.log(position); // Displays: 3

  // Verification
  console.log(array[position]); // Displays: 40
}

This method can also be used to determine whether a value is present in the array or not (and returns -1 if there is no result):

function test() {

  const array = [10, 20, 30, 40, 50];

  // Position of the number 60
  const position = array.indexOf(60);

  // Message depending on the result
  console.log(position == -1 ? 'No result' : 'Result = ' + position); // Displays: No result
}

Contains

The includes method also allows you to know if a value is present or not in the array (and returns true or false):

function test() {

  const array = [10, 20, 30, 40, 50];

  // If the array contains the value 30
  if (array.includes(30)) {
    console.log('YES'); // Displayed
  }
}

Copying an Array

To copy an array, you cannot simply enter array2 = array1, because remember that in this case, only the reference to the array is passed to array2.

To create a copy of an array, simply use the slice method (without argument):

function test() {

  const array1 = [10, 20, 30, 40, 50];
  const array2 = array1.slice();

  array2.push(60);

  console.log(array1); // Displays: [ 10, 20, 30, 40, 50 ]
  console.log(array2); // Displays: [ 10, 20, 30, 40, 50, 60 ]
}

Note that the slice method also allows copying only a portion of an array:

function test() {

  const array = [10, 20, 30, 40, 50];

  console.log(array.slice(0, 3)); // Displays: [ 10, 20, 30 ]
  console.log(array.slice(1)); // Displays: [ 20, 30, 40, 50 ]
  console.log(array.slice(-2)); // Displays: [ 40, 50 ]
}

Decomposing an Array

To decompose an array, or in other words, "extract" the values from the array, use the ... operator. Here is an example to understand better:

function test() {

  const array = [10, 20, 30, 40, 50];

  console.log(array); // Displays: [ 10, 20, 30, 40, 50 ]
  console.log(...array); // Displays: 10 20 30 40 50
}

You can use it, for example, to copy an array (by extracting the values of the array, inside a new array):

function test() {

  const array1 = [10, 20, 30, 40, 50];
  const array2 = [...array1];

  array2.push(60);

  console.log(array1); // Displays: [ 10, 20, 30, 40, 50 ]
  console.log(array2); // Displays: [ 10, 20, 30, 40, 50, 60 ]
}

It can also be used to add the values of one array into another array (and in this example, without this operator, it's the array that is added and not the values of the array):

function test() {

  const array1 = [10, 20, 30, 40, 50];
  const array2 = [60, 70, 80, 90, 100];

  array1.push(...array2);

  console.log(array1); // Displays: [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ]
  console.log(array2); // Displays: [ 60, 70, 80, 90, 100 ]
}

Or you can create a new array with the contents of both arrays:

function test() {

  const array1 = [10, 20, 30, 40, 50];
  const array2 = [60, 70, 80, 90, 100];
  const array3 = [...array1, ...array2];

  console.log(array1); // Displays: [ 10, 20, 30, 40, 50 ]
  console.log(array2); // Displays: [ 60, 70, 80, 90, 100 ]
  console.log(array3); // Displays: [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ]
}

Array

To create an array with a defined number of empty slots, use Array:

function test() {

  const array = Array(10);

  array[7] = 'Test';

  console.log(array); // Displays: [ , , , , , , , 'Test', ,  ]
}

Joining

To join the values of an array and obtain a string, use the join method (the equivalent of the Google Sheets JOIN function):

function test() {

  const array = [10, 20, 30, 40, 50];

  console.log(array.join(' / ')); // Displays: 10 / 20 / 30 / 40 / 50
}

Splitting into an Array

To split a string into an array, use the split method (the equivalent of the Google Sheets SPLIT function):

function test() {

  const text = '10 / 20 / 30 / 40 / 50';

  console.log(text.split(' / ')); // Displays: [ '10', '20', '30', '40', '50' ]
}

To split each character of a string into an array, use the split method (or the ... operator in an array):

function test() {

  const text = 'Sheets-Pratique';

  console.log(text.split('')); // Displays: [ 'S', 'h', 'e', 'e', 't', 's', '-', 'P', 'r', 'a', 't', 'i', 'q', 'u', 'e' ]
  console.log([...text]); // Displays: [ 'S', 'h', 'e', 'e', 't', 's', '-', 'P', 'r', 'a', 't', 'i', 'q', 'u', 'e' ]
}