Find the Max Length of Nested Arrays


What Does This Code Snippet Do?

This findMaxLengthOfNestedArrays() code snippet takes an array of arrays (aka nested arrays) and calculates the longest array out of all of them. It does this by using the Math utility's max() method which lets it quickly find the largest value within the array. To iterate through the nested array, we do a .map() to list out all of the lengths of each array found within the collection.

Why Is It Useful?

I originally designed this code snippet to help determine what the data grid size should be to prevent data import failures into a Google Sheet. When you're importing data into a Google Sheet via Apps Script (e.g. pulling data from an API or transfering data from one sheet to another), the data grid you're using must be a square. Meaning, every array must be the same size.

Let's visualize it for a second:

let bad_array = [ [1,2,3], [4,5,6], [7,8] ] let good_array = [ [1,2,3], [4,5,6], [7,8,9] ]

If you tried to import bad_array into your Google Sheet, it would fail because the 3rd array in your nested array is a different length than the others. good_array on the other hand would import successfully since all of the arrays are equal in size.



How to Use This Code Snippet

To use this code snippet, first copy in the function into your Google Apps Script project - you can put it anywhere you like.

function findMaxLengthOfNestedArrays(nestedArray) { return Math.max(...nestedArray.map(row => row.length)) }

From there, just call the findMaxLengthOfNestedArrays() and pass through your nested array. Like this:

let sample_array = [ [1,2,3,5,6,7,8,9], [1,2,3,5], [1,2,3,5,6,7,8,9,0], [1,2,3,5,6,7,8] ] findMaxLengthOfNestedArrays(sample_array) // Output: 9



BONUS - How to Fix an Uneven Data Grid

A quick way to fix an uneven data grid so that you can import it into your Google Sheet would be to inject blank values to make up the difference in array sizes. You can do this simply be iterating through the nested array and adding a loop in each iteration to insert blanks until the array is the same size as the biggest array in your nested array.

Here's some simple logic that does just this:

let sample_array = [ [1,2,3,4,5,6,7,8,9], [1,2,3,4,5], [1,2,3,4,5,6,7,8,9,0], [1,2,3,4,5,6,7,8] ] let maxLength = findMaxLengthOfNestedArrays(sample_array) let cleaned_array = [] for(item in sample_array) { let row = sample_array[item] let cleaned_row = [] console.log(row) for(i=0; i < maxLength; i++) { if(i < row.length) { cleaned_row.push(row[i]) } else { cleaned_row.push('') } } cleaned_array.push(cleaned_row) } console.log(cleaned_array) // Output: [ // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, '' ], // [ 1, 2, 3, 4, 5, '', '', '', '', '' ], // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ], // [ 1, 2, 3, 4, 5, 6, 7, 8, '', '' ] // ]
thumbnail

NitroGAS Chrome Extension

Want to reference this code snippet right from your Google Apps Script Project? Check out our NitroGAS Chrome Extension. This tool will help you build your scripts faster than you could imagine. The tool itself and access to the code snippets are 100% FREE - Happy Coding!

Get the Extension