Pages

Tuesday, 11 December 2018

Array Splice

The following Google Apps Script is designed to remove 'undefined' values from a 1-D array. It has been developed as an independent function with an existing array as its input parameter - 'dataArray'. Be aware that it will ultimately change the length of the array so it is not suited for writing values back to a spreadsheet row for instance - as the data will no longer match the number of columns.

The code will loop through each item in the array, use 'indexOf' to find the position of it and return a value (otherwise it always returns "-1"). 'Splice' can then use this value to find the position of the string in the array and remove it, ultimately returning a 'cleansed' array for you to use.

Example
Input array: ['one', undefined, 'two', null, undefined]
Output array: [one, two, null]

No comments:

Post a comment