简体   繁体   English

Javascript - 用于展平数组的递归/for循环

[英]Javascript - recursion/for loop for flattening array

So, here is a sample solution to solve the problem of flattening an array.因此,这里有一个解决数组展平问题的示例解决方案。 My question is not 'how' to flatten the array.我的问题不是“如何”展平阵列。 Rather, I am trying to understand some underlying functionality occurring in this recursion.相反,我试图了解此递归中发生的一些底层功能。

This solution goes through each element of the original array, breaking down any elements that are arrays by putting them back through the function until they are no longer arrays and can be pushed to the new array.该解决方案遍历原始数组的每个元素,通过将它们放回函数中来分解任何数组元素,直到它们不再是数组并且可以推送到新数组。

My question is, how does the 'for' loop keep track of all of the times that an element is put back through the function, and also continue looping through the rest of the 'original' array that it is working on at the time?我的问题是,“for”循环如何跟踪元素通过函数放回的所有时间,并继续循环遍历它当时正在处理的“原始”数组的其余部分? It must be keeping track somehow otherwise every time an element is an array and is put back through, the current loop would be cut short.它必须以某种方式保持跟踪,否则每次元素是一个数组并被放回时,当前循环都会被缩短。 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]]]]);

So, here is a sample solution to solve the problem of flattening an array.因此,这是解决阵列扁平化问题的示例解决方案。 My question is not 'how' to flatten the array.我的问题不是“如何”展平阵列。 Rather, I am trying to understand some underlying functionality occurring in this recursion.相反,我试图了解此递归中发生的一些基本功能。

This solution goes through each element of the original array, breaking down any elements that are arrays by putting them back through the function until they are no longer arrays and can be pushed to the new array.此解决方案遍历原始数组的每个元素,通过使它们回到函数中来分解数组中的所有元素,直到它们不再是数组并可以推入新数组为止。

My question is, how does the 'for' loop keep track of all of the times that an element is put back through the function, and also continue looping through the rest of the 'original' array that it is working on at the time?我的问题是,“ for”循环如何跟踪通过函数放回元素的所有时间,并继续循环遍历当时正在处理的“原始”数组的其余部分? It must be keeping track somehow otherwise every time an element is an array and is put back through, the current loop would be cut short.它必须以某种方式保持跟踪,否则,每当一个元素是一个数组并放回原位时,当前循环将被缩短。 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]]]]);

So, here is a sample solution to solve the problem of flattening an array.因此,这是解决阵列扁平化问题的示例解决方案。 My question is not 'how' to flatten the array.我的问题不是“如何”展平阵列。 Rather, I am trying to understand some underlying functionality occurring in this recursion.相反,我试图了解此递归中发生的一些基本功能。

This solution goes through each element of the original array, breaking down any elements that are arrays by putting them back through the function until they are no longer arrays and can be pushed to the new array.此解决方案遍历原始数组的每个元素,通过使它们回到函数中来分解数组中的所有元素,直到它们不再是数组并可以推入新数组为止。

My question is, how does the 'for' loop keep track of all of the times that an element is put back through the function, and also continue looping through the rest of the 'original' array that it is working on at the time?我的问题是,“ for”循环如何跟踪通过函数放回元素的所有时间,并继续循环遍历当时正在处理的“原始”数组的其余部分? It must be keeping track somehow otherwise every time an element is an array and is put back through, the current loop would be cut short.它必须以某种方式保持跟踪,否则,每当一个元素是一个数组并放回原位时,当前循环将被缩短。 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]]]]);

So, here is a sample solution to solve the problem of flattening an array.因此,这是解决阵列扁平化问题的示例解决方案。 My question is not 'how' to flatten the array.我的问题不是“如何”展平阵列。 Rather, I am trying to understand some underlying functionality occurring in this recursion.相反,我试图了解此递归中发生的一些基本功能。

This solution goes through each element of the original array, breaking down any elements that are arrays by putting them back through the function until they are no longer arrays and can be pushed to the new array.此解决方案遍历原始数组的每个元素,通过使它们回到函数中来分解数组中的所有元素,直到它们不再是数组并可以推入新数组为止。

My question is, how does the 'for' loop keep track of all of the times that an element is put back through the function, and also continue looping through the rest of the 'original' array that it is working on at the time?我的问题是,“ for”循环如何跟踪通过函数放回元素的所有时间,并继续循环遍历当时正在处理的“原始”数组的其余部分? It must be keeping track somehow otherwise every time an element is an array and is put back through, the current loop would be cut short.它必须以某种方式保持跟踪,否则,每当一个元素是一个数组并放回原位时,当前循环将被缩短。 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]]]]);

So, here is a sample solution to solve the problem of flattening an array.因此,这是解决阵列扁平化问题的示例解决方案。 My question is not 'how' to flatten the array.我的问题不是“如何”展平阵列。 Rather, I am trying to understand some underlying functionality occurring in this recursion.相反,我试图了解此递归中发生的一些基本功能。

This solution goes through each element of the original array, breaking down any elements that are arrays by putting them back through the function until they are no longer arrays and can be pushed to the new array.此解决方案遍历原始数组的每个元素,通过使它们回到函数中来分解数组中的所有元素,直到它们不再是数组并可以推入新数组为止。

My question is, how does the 'for' loop keep track of all of the times that an element is put back through the function, and also continue looping through the rest of the 'original' array that it is working on at the time?我的问题是,“ for”循环如何跟踪通过函数放回元素的所有时间,并继续循环遍历当时正在处理的“原始”数组的其余部分? It must be keeping track somehow otherwise every time an element is an array and is put back through, the current loop would be cut short.它必须以某种方式保持跟踪,否则,每当一个元素是一个数组并放回原位时,当前循环将被缩短。 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]]]]);

So, here is a sample solution to solve the problem of flattening an array.因此,这是解决阵列扁平化问题的示例解决方案。 My question is not 'how' to flatten the array.我的问题不是“如何”展平阵列。 Rather, I am trying to understand some underlying functionality occurring in this recursion.相反,我试图了解此递归中发生的一些基本功能。

This solution goes through each element of the original array, breaking down any elements that are arrays by putting them back through the function until they are no longer arrays and can be pushed to the new array.此解决方案遍历原始数组的每个元素,通过使它们回到函数中来分解数组中的所有元素,直到它们不再是数组并可以推入新数组为止。

My question is, how does the 'for' loop keep track of all of the times that an element is put back through the function, and also continue looping through the rest of the 'original' array that it is working on at the time?我的问题是,“ for”循环如何跟踪通过函数放回元素的所有时间,并继续循环遍历当时正在处理的“原始”数组的其余部分? It must be keeping track somehow otherwise every time an element is an array and is put back through, the current loop would be cut short.它必须以某种方式保持跟踪,否则,每当一个元素是一个数组并放回原位时,当前循环将被缩短。 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]]]]);

So, here is a sample solution to solve the problem of flattening an array.因此,这是解决阵列扁平化问题的示例解决方案。 My question is not 'how' to flatten the array.我的问题不是“如何”展平阵列。 Rather, I am trying to understand some underlying functionality occurring in this recursion.相反,我试图了解此递归中发生的一些基本功能。

This solution goes through each element of the original array, breaking down any elements that are arrays by putting them back through the function until they are no longer arrays and can be pushed to the new array.此解决方案遍历原始数组的每个元素,通过使它们回到函数中来分解数组中的所有元素,直到它们不再是数组并可以推入新数组为止。

My question is, how does the 'for' loop keep track of all of the times that an element is put back through the function, and also continue looping through the rest of the 'original' array that it is working on at the time?我的问题是,“ for”循环如何跟踪通过函数放回元素的所有时间,并继续循环遍历当时正在处理的“原始”数组的其余部分? It must be keeping track somehow otherwise every time an element is an array and is put back through, the current loop would be cut short.它必须以某种方式保持跟踪,否则,每当一个元素是一个数组并放回原位时,当前循环将被缩短。 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]]]]);

 let array = [1, [2], [3, [[4]]]]; const final = []; const stack = []; for (let i = 0; i < array.length; i++) { const ele = array[i]; stack.push(ele); while (stack.length) { const first = stack.shift(); if (Array.isArray(first)) { first.forEach(ele => stack.push(ele)) } else { final.push(first) } } } console.log( final.join(', '));<\/code><\/pre>

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM