#ActuallyAutistic web dev. Does front of the front-end. Loves perf and minimalism. Prefers HTML, CSS, Web Standards over JS, UX over DX. Hates div disease.
functionflatten(array){// only mutate what you own, thus create a new copyarray=array.slice(0);for(i=0;i<array.length;i++){// Array.isArray(array[i])if(Object.prototype.toString.call(array[i])==='[object Array]'){// replaces single item with all items from the arrayArray.prototype.splice.apply(array,[i,1].concat(array[i--]));}}returnarray;}
This should work with pretty much any JavaScript engine released within last 20 years, and does it without recursion.
But as others are saying you should just use .flat() these days instead of reinventing the wheel.
The good old super compatible way to do it:
This should work with pretty much any JavaScript engine released within last 20 years, and does it without recursion.
But as others are saying you should just use
.flat()
these days instead of reinventing the wheel.Thanks for reading the article and for this solution 🙌. Really appreciate it.
But yeah I covered this solution because it covers recursion as well.