Skip to content
Advertisement

Convert a simple array to a two dimensional array of objects

I want to convert this:

[null, 1890, null, NGU]

…into this:

[[], [1890], [], [NGU]]

I’ve tried creating a new array and pushing values to it, but that just ends up looking the same. Honestly, I’m unsure of what to even call what I’m trying to create. Is it a two-dimensional array or an array of objects?

This is for a google app script and the documentation calls it a two-dimensional array of values.

Advertisement

Answer

var arr = [null, 1890, null, 'NGU']
var arr2d = arr.map(x => [x])

console.log(arr2d) // output --> [ [ null ], [ 1890 ], [ null ], [ 'NGU' ] ]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement