John Croneh John Croneh - 4 months ago 12
Javascript Question

JavaScript: Convert [a,b,c] into [a][b][c]

I have arrays like

[a]
,
[a,b]
,
[a,b,c]
and so on.

How can I convert them into
[a]
,
[a][b]
,
[a][b][c]
and so on?

Example:

var arr = [1,2,3,4];
arr = do(arr); // arr = arr[1][2][3][4]

Answer

You could map it with Array#map.That returns an array with the processed values.

ES6

console.log([1, 2, 3, 4].map(a => [a]));

ES5

console.log([1, 2, 3, 4].map(function (a) {
    return [a];
}));