Hello Folks,
I’m having a hard time to concat Array (n-Dimensional) without actualy using concat (for training).
const multiArray = [[1,2,3],[4,5,6,[6,7,8,],[7,8,9]],['2','3','4']];
let newArray =[];
function readArray(input,output){
for (let i=0; input.length>i; i++){
if (Array.isArray(input[i]))
{
readArray(input[i],output)
} else {
output.push(input[i]);
}
}
}
readArray(multiArray, newArray);
console.log(newArray) //[ 1, 2, 3, 4, 5, 6, 6, 7, 8, 7, 8, 9, '2', '3', '4' ]
This is what I got so far, it works fine but is gritty, because i have to use a hoisted scope for the new array and giving it to the parameters. Instead i’d like it as the return value [newArray] - any Ideas?
As this is a recursive function I do not know where to return and how to merge the Arrays from earlier recursive steps.