Concat Array (n-Dimensional) without actualy using concat

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.

Just thinking on this, one conclusion I’ve drawn is that the only way to not use a global array is to return an array. That means using a call stack. Each recursion needs to return something, and a base case will return to the caller, which will wind down the stack. The net result will be the flattened array.

Thanks for sharing your thoughts, currently I’m learning the basics for JS - how to put it in the call stack and using return without stopping the function in its track?
If there is some method/function like “jmp” to an anchor or line i could create the recursion without recalling the fuction, thus ending it only once with return.

Recursion is a rather advanced topic that may best be explored somewhere down the road when all the basic concepts are cemented in your thinking. That being said, let’s consider a recursion for Factorial…

function factorial(n) {
    if (n < 0) {
        return 0;
    }
    if (n === 0 || n === 1) {        // base case
        return 1;
    } else {
        return n * factorial(n - 1)  // recursive case
    }
}

console.log(factorial(7))            // <- 5040

There are other topics on recursion in the forums which may help illustrate and explain the above, and the concept in general. The thing to note is that every case returns something, namely another call (except for the base case).

Hello, @web4547361878.

You could nest a function within a function like so:

const flatten = arrayToFlatten => {
  //flattens an array of nested arrays regardless of nesting levels
    
    const flatArray = []
    
    const flat = arr => {
      for(let e of arr) {
        if(Array.isArray(e)) {
          flat(e)
        } else {
          flatArray.push(e)
        }
      }
    }
    
    flat(arrayToFlatten)    
    return flatArray
}

//Test
const myArray = [[1,2, [3, 'four', ['five', false], true], 8], 'nine', 10, [11, 12, [13]]]
const myFlatArray = flatten(myArray)
console.log(myFlatArray)

Output:

[ 1, 2, 3, ‘four’, ‘five’, false, true, 8, ‘nine’, 10, 11, 12, 13 ]

:hugs::+1:Thanks for this hint, the nested function works fine. No more hoisted variables, and a clean output.
could have thougth of this earlier.:persevere:

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.