使用javascript中的递归函数展平数组

Javascript can seem daunting, and when dealing with complex problems such as flattening an array, the answer can seem unintuitive at best. However, there is a simple way that the problem of arrays in arrays can be solved, and that’s using Recursive Functions.

Javascript似乎令人生畏,当处理复杂的问题(例如,将数组展平)时,答案似乎充其量是不直观的。 但是,有一种简单的方法可以解决数组中的数组问题,并且使用了递归函数。

To begin, let me explain what an “Unflattened” array looks like. An unflattened array is an array that contains arrays within it. Think of it like peaks and valleys, where the lowest level array contains all other arrays within, and those arrays also contain arrays within them. It will look something like this in code:

首先,让我解释一下“未展平”数组的外观。 未展平的数组是其中包含数组的数组。 可以将其视为峰和谷,其中最低级别的数组包含其中的所有其他数组,而这些数组也包含其中的数组。 在代码中看起来像这样:

let unflatArray = [55, 23, [45, 7, [9, 2, [67], 6, 1], 54, 23, [99, 92], 45]]

让unflatArray = [55,23,[45,7,[9,2,[67],6,1],54,23,[99,92],45]]

As you can see, there are numbers contained on each level, building up to the “Top” with 67, though there is another smaller peak where 99 and 92 are. But how does go through the base array, then all the arrays within, one by one? That’s where Recursive functions come in.

如您所见,每个级别上都有一些数字,最高的是67,尽管还有另一个较小的峰值,即99和92。 但是如何遍历基本数组,然后遍历其中的所有数组? 这就是递归函数出现的地方。

A recursive function is a function that calls on itself. There is the first call to the function, then many calls and repetitions of the same function within itself, depending on how the recursive function gets called again. It’s perfect for unflattened arrays because we can choose when to sequentially go through one array, then pause that midway through once we run into another array and start doing that array, rather than trying to organize the unflattened array, we’re just going to traverse it like any other array, just in much smaller bite-sized chunks. Let’s have a look at how this is constructed:

递归函数是一个自行调用的函数。 首先是对函数的调用,然后是内部相同函数的多次调用和重复,这取决于递归函数如何再次被调用。 它非常适合未展平的数组,因为我们可以选择何时顺序遍历一个数组,然后在遇到另一个数组并开始执行该数组时中途暂停,而不是尝试组织未展平的数组,我们只是遍历就像任何其他数组一样,只是大小更小。 让我们看一下它是如何构造的:

let unflatArray = [55, 23, [45, 7, [9, 2, [67], 6, 1], 54, 23, [99, 92], 45]]
let flatArray = []


console.log(unflatArray)


function flattenArray(array) {
  for (let i = 0; i < array.length; i++) {
    if (typeof array[i] === "number") {
      flatArray.push(array[i])
    } else {
      flattenArray(array[i])
    }
  
  }
}


flattenArray(unflatArray)


console.log(flatArray)

As you can see, the function itself is actually extremely simple. What we’re doing is creating a function that will take an array. It will then iterate over that array. For each iteration, if the type of the current array item is a number, it will add that number to the new flatArray list. However, if it does not see a number, it will run the flattenArray function from within the flattenArray function. Each time a new iteration of the function is running, it will handle that function first before going back to the for loop in the previous iteration of flattenArray. That way, the end result is a single array with the numbers in the same order that they came in as in the unflattened array. You may also change the “typeof” conditional to equal other data types like strings or booleans, to fit your individual needs.

如您所见,函数本身实际上非常简单。 我们正在做的是创建一个将采用数组的函数。 然后它将遍历该数组。 对于每次迭代,如果当前数组项的类型是数字,它将将该数字添加到新的flatArray列表中。 但是,如果它没有看到一个数字,它将从flattenArray函数运行flattenArray功能。 每次运行该函数的新迭代时,它将首先处理该函数,然后再返回FlattenArray的先前迭代中的for循环。 这样,最终结果是单个数组,其编号与未展平数组中的编号顺序相同。 您也可以将条件的“ typeof”更改为等于其他数据类型,例如字符串或布尔值,以满足您的个人需求。

Nevertheless, this simple usage of Recursive Functions might even land you a job at Facebook, as this is a common technical interview question that they ask of their prospective employees. Keep it in mind!

尽管如此,递归函数的这种简单用法甚至可能使您在Facebook上找到工作,因为这是他们向准员工提出的常见技术面试问题。 记在心上!

翻译自: https://medium.com/@rinerkyle97/flattening-an-array-using-recursive-functions-in-javascript-5c80fd5544aa

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值