Reputation: 151
I have this 2 dimensional array:
var list = [
['zone_1', 'zone_2'],
['zone_3']
]
I want to merge all elements in the sub-arrays into a single array:
var list = [
'zone_1',
'zone_2',
'zone_3'
]
How can I do that in node.js? It is possible to do it without using a loop or map?
Upvotes: 2
Views: 7981
Reputation: 81
Array.prototype.flat() The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
const arr = [ ['zone_1', 'zone_2'], ['zone_3'] ];
console.log(arr.flat());
Upvotes: 8
Reputation: 32176
The array .concat
method is variadic, and you can use the spread operator to pass each sub-array to it as a separate argument. This makes flattening an array turn into a nice one-liner:
const arr = [ ['zone_1', 'zone_2'], ['zone_3'] ];
console.log([].concat(...arr))
Upvotes: 9