Javascript - recursion / for loop to align array

So, here is an example solution to solve the problem of smoothing the array. My question is not how to smooth the array. Rather, I am trying to understand some basic functions that arise in this recursion.

This solution goes through each element of the original array, breaking up any elements that are arrays, returning them through the function until they are no longer arrays and can be transferred to a new array.

My question is, how does the "for" loop keep track of all the moments when an element returns through a function, and also continues the loop through the rest of the "source" array that it is working on time? It must track in some way every time an element is an array and returns back, the current loop will be interrupted. Hope my question makes sense.

function steamrollArray(array) {
  var flatArray = [];

  flatten(array);

  function flatten(array) {
    for (var i = 0; i < array.length; i++) {
      if (Array.isArray(array[i])) {
        flatten(array[i]);
      } else {
        flatArray.push(array[i]);
      }
    }
  }

  return flatArray;
}
steamrollArray([1, [2], [3, [[4]]]]);
+4
source share
5 answers

I think there will be better answers, but here goes ...

At a minimum, do not think of it as a "violation" of the loop, think that it continues to execute code in a procedural manner. Therefore, from within the context of the loop, it calls itself as a function, when this function is completed, it continues in the loop. Example

var item = [1, [2, 3], 4]

flatten(item) :

Loop 1: 
  Push 1
Loop 2: 
  Start a call to flatten with [2, 3]
  Loop 1: 
    push 2
  Loop 2: 
    push 3
  End loop
Loop 3: 
  Push 4
End Loop.

, , . "", , , javascript , .

, .

+3

flatArray , , , , , , , , , .

, flatten(array[i]), . , , , . return.

:

function test () {
  console.log("hello, ");
  console.log("world!");
}

, console.log("hello, ") (console.log("world!")). , , . , .

+2

, . , , .

, flatArray. , - , . , , , THAT- .. ..

: [1, [2], [3, [[4]]]]

  • index 0 - 1, , flatArray
  • index 1 - , - , 2 -
  • index 2 - , , 0 - 3, . - , , - - , 4, ..
+1

" " , ; javascript , .

, , , , flatten .

function steamrollArray(array) {
  var flatArray = [];
  flatten(array, flatArray);
  return flatArray;
}

function flatten(array, flatArray) {
  flatArray = flatArray || [];

  for (var i = 0; i < array.length; i++) {
    if (Array.isArray(array[i])) {
      flatten(array[i]);
    } else {
      flatArray.push(array[i]);
    }
  }
}

steamrollArray([1, [2], [3, [[4]]]]); # [1, 2, 3, 4]

, flatArray flatten. steamrollArray ... .

function flatten(array, flatArray) {
  flatArray = flatArray || [];

  for (var i = 0; i < array.length; i++) {
    if (Array.isArray(array[i])) {
      flatten(array[i], flatArray);
    } else {
      flatArray.push(array[i]);
    }
  }

  return flatArray;
}

flatten([1, [2], [3, [[4]]]]);  # [1, 2, 3, 4]

, . , "", . , , . , flatten for, i flatArray. flatArray flatten, .

+1

, .

function flattenArray(arr){

  for(var i=0;i<arr.length;i++){

    if(arr[i] instanceof Array){

      Array.prototype.splice.apply(arr,[i,1].concat(arr[i]))
       i--;
    }

  }

  return arr;
}
0

All Articles