Last active
February 4, 2018 20:19
-
-
Save mayashavin/1b1cec3497f0e9bf5ba709e82c957f7b to your computer and use it in GitHub Desktop.
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
function FlattenArray(arr){ | |
var flattened = []; | |
while(arr.length){ | |
var element = arr.pop(); //start from end | |
//if (element instanceof Array) | |
//if (Object.prototype.toString.call(element) === '[object Array]') | |
if (Array.isArray(element)){ | |
arr = arr.concat(element); | |
} | |
else{ | |
flattened.push(element); | |
} | |
} | |
return flattened.reverse(); | |
} |
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
function FlattenArray(arr){ | |
var flattened = []; | |
for (var i = 0; i < arr.length; i++){ | |
var element = arr[i]; | |
if (Array.isArray(element)){ | |
flattened = flattened.concat(FlattenArray(element)); | |
} | |
else{ | |
flattened.push(element); | |
} | |
} | |
return flattened; | |
} |
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
function FlattenArray(arr){ | |
var reducer = (flatten, original) => flatten.concat((Array.isArray(original) ? FlattenArray(original) : original)); | |
return arr.reduce(reducer, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment