简体   繁体   English

Javascript数组高阶函数

[英]Javascript array higher-order functions

Can anyone explain to me how this code works? 谁能向我解释此代码的工作原理? I looked for reduce and concat functions in Array, I understand these functions but I don't understand how this code works initialy: 我在Array中寻找了reduceconcat函数,我理解这些函数,但是我不理解这段代码最初是如何工作的:

 var arrays = [[1, 2, 3], [4, 5], [6]]; console.log(arrays.reduce(function(flat, current) { return flat.concat(current); }, [])); // → [1, 2, 3, 4, 5, 6] 

You could add a console.log inside the callback we pass to the reduce and think about the output: 您可以在传递给reduce的回调中添加console.log并考虑输出:

 var arrays = [[1, 2, 3], [4, 5], [6]]; console.log(arrays.reduce(function(flat, current) { console.log('flat: '+ flat + 'current: ' + current) return flat.concat(current); }, [])); 

Initially we concat an empty array and the array [1,2,3]. 最初,我们连接一个空数组和数组[1,2,3]。 So the result is a new array with elements [1,2,3]. 因此,结果是一个包含元素[1,2,3]的新数组。 Then we concat this array with the next element of the arrays , the array [4,5]. 然后我们Concat的这个阵列与所述的下一个元素arrays ,阵列[4,5]。 So the result would be a new array with elements [1,2,3,4,5]. 因此,结果将是一个包含元素[1,2,3,4,5]的新数组。 Last we concat this array with the last element of the arrays , the array [6]. 最后我们Concat的这个阵列与所述的最后一个元素arrays ,所述array [6]。 Hence the result is the array [1,2,3,4,5,6]. 因此,结果是数组[1,2,3,4,5,6]。

Ir order to understand in details the above you have to read about Array.prototype.reduce() . 为了详细了解上述内容,您必须阅读有关Array.prototype.reduce()的信息

As it is stated in the above link: 如以上链接所述:

The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value reduce()方法对一个累加器应用一个函数,该数组的每个值(从左到右)将其减小为单个值

Furthermore the syntax is 此外,语法是

arr.reduce(callback, [initialValue])

In you case the initialValue is an empty array, [] . 在这种情况下,initialValue是一个空数组[]

I've described each step for you. 我已经为您介绍了每个步骤。

 var arrays = [[1, 2, 3], [4, 5], [6]]; console.log(arrays.reduce(function(flat, current) { // first loop: flat - [1,2,3], current - [4,5] // [1,2,3].concat([4,5]) -> [1,2,3,4,5] //second/last loop: flat - [1,2,3,4,5], current - [6] // [1,2,3,4,5].concat([6]) -> [1,2,3,4,5,6] //function stop return flat.concat(current); }, [])); 

Well actually it's a wrong use of .reduce() . 好吧,实际上这是对.reduce()的错误使用。 For this job you don't need no initial array. 对于这项工作,您不需要初始数组。 Just the previous ( p ) and current ( c ) hand to hand can do it. 仅前一个( p )和当前( c )可以做到。 Such as; 如;

 var arrays = [[1, 2, 3], [4, 5], [6]]; console.log(arrays.reduce((p,c) => p.concat(c))); 

Note: Initial is handy when the type of the returned value is different from the array items. 注意:当返回值的类型与数组项不同时,Initial很方便。 However in this case you are processing arrays and returning an array which renders the use of initial redundant. 但是,在这种情况下,您要处理数组并返回一个数组,该数组将使用初始冗余。

So assuming we have the 2D array that you do: [[1, 2, 3], [4, 5], [6]] that is being reduced, the function is split into 2 main components. 因此,假设我们拥有要执行的2D数组: [[1, 2, 3], [4, 5], [6]]被缩减,则函数将分为2个主要部分。

array.reduce((accumulator, iterator) => {...}, initialValue);
  • flat - this is the accumulator of the reduction. flat -这是减少量的累加器。 It is given the initial value as passed into the second parameter of the reduce function and is used to store the values as the iterator passes through them. 传递给reduce函数的第二个参数的初始值是给定的,并用于在迭代器通过它们时存储这些值。
  • current - this is the iterator that goes through all values within the data set being reduced. current -这是遍历要减少的数据集中所有值的迭代器。

So as you're iterating through the data set, your example is concatenating the accumulation array with the current value, and by the end you have your new array. 因此,当您遍历数据集时,您的示例将累积数组与当前值连接在一起,最后您有了新数组。

Array.reduce expects a callback with following signature: Array.reduce需要具有以下签名的回调:

function(previousElement, currentElement, index, array) 函数(previousElement,currentElement,索引,数组)

and an optional initial value. 和一个可选的初始值。

In first iteration, if initialValue is passed, then previousElement will hold this value and currentElement will hold `firstArrayElement. 在第一次迭代中,如果传递了initialValue ,则previousElement将保留此值,而currentElement将保留`firstArrayElement。

If not, then previousElement will hold firstArrayElement and currentElement will hold secondArrayElement . 如果不是,则previousElement将保存firstArrayElementcurrentElement将保存secondArrayElement

For the following iterations, previousElement will hold value returned by previous iteration and currentElement will hold next value. 对于以下迭代, previousElement将保存由先前迭代返回的值,而currentElement将保存下一个值。


So in you example, initially flat holds [] . 因此,在您的示例中,最初的flat []

return flat.concat(current); will return a new merged array. 将返回一个新的合并数组。 This value will be used as flat for next iteration, and this process is returned. 该值将用作下一次迭代的flat ,并返回此过程。 Finally, value returned by last iteration is used as final return value and is printed in console. 最后,最后一次迭代返回的值用作最终返回值,并打印在控制台中。

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

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