Created
March 15, 2016 18:18
-
-
Save AquiGorka/4ef41303e233b23b54ae to your computer and use it in GitHub Desktop.
Flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4].
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.replaceAll = function(search, replacement) { | |
var target = this; | |
return target.split(search).join(replacement); | |
}; | |
Array.prototype.flatten = function() { | |
return JSON.stringify(this).replaceAll('[','').replaceAll(']', '').split(',').map(item => parseInt(item)); | |
}; | |
var a = [[1,2,[3]],4]; | |
console.log(a.flatten()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment