Merge/flatten an array of arrays
P粉828463673
2023-08-27 12:05:43
<p>I have a JavaScript array, for example: </p>
<pre class="brush:php;toolbar:false;">[["$6"], ["$12"], ["$25"], ["$25"], ["$18"], [" $22"], ["$10"]]</pre>
<p>How do I combine separate internal arrays into one array like this: </p>
<pre class="brush:php;toolbar:false;">["$6", "$12", "$25", ...]</pre>
<p><br /></p>
This is a short function that uses some of the newer JavaScript array methods to flatten an n-dimensional array.
usage:
ES2019
ES2019 introduces
arrays. prototype.flat()
method, which you can use to flatten an array. It is compatible with most environments, although it is only available in Node.js starting with version 11 and not within Node.js. It's totally fine in Internet Explorer.