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
/* | |
Example of a flatten array method using recursion. If the original array is | |
large do NOT use this funtion. Too much memory is used off the stack. | |
*/ | |
let array = [[1,2,[3]],4]; | |
let newArray = flattenRecursive(array); | |
function flattenRecursive(array, results = []) { | |
for (let i = 0; i < array.length; i++) { |
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
let array = [[1,2,[3],4],[5, 6,[7]]]; | |
let newArray = flatten(array); | |
function flatten(array) { | |
if (!array || !array.length) return []; | |
let stack = [], | |
indexes = [], | |
currentIdx = 0, | |
results = []; |