by
10
7
181k
1
Top 1% !
Popular
Pearl of Wisdom
Nice
Easy-to-find
Specified
MultiPlatform
Popularity: 9554th place
This snippet is Public
Languagejavascript
LicenseMIT_X11

"flatten" your complex arrays

a flattening Array helper function
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/231638/-flatten--your-complex-arrays?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// go flat yerself! function flatten(arrayToFlatten, rslt) { rslt = rslt || []; arrayToFlatten.forEach(function(itm) { if (Array.isArray(itm)){ rslt = flatten(itm, rslt); } else { rslt.push(itm); } }); return rslt; } // use it... var someArrayData = [[60, 121, 4, 832],[71, 188, 6, 33]]; // [60, 121, 4, 832, 71, 188, 6, 33] console.log(flatten(someArrayData));
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus