01 - Execise About Array.prototype.reduce()

Description:

Write a generic function chainer

Write a generic function chainer that takes a starting value, and an array of functions to execute on it (array of symbols for ruby).

The input for each function is the output of the previous function (except the first function, which takes the starting value as it‘s input). Return the final value after execution is complete.

 1 function add(num) {
 2   return num + 1
 3 }
 4
 5 function mult(num) {
 6   return num * 30
 7 }
 8
 9 chain(2, [add, mult]);
10 // returns 90;

我的答案是:

function chain(input, fs) {
  var result = input;
  for(var i = 0; i < fs.length; i++) {
    result = fs[i](result);
  }
  return result;
}

最优解是:

function chain(v, fns) {
  return fns.reduce(function(v, fn) { return fn(v) }, v);
}

然后引发了对reduce方法的关注,在MDN中

Array.prototype.reduce()的语法如下:arr.reduce(callback[,initialValue]);

callback
Function to execute on each value in the array, taking four arguments:

previousValue
The value previously returned in the last invocation of the callback, or initialValue, if supplied. (See below.)
currentValue
The current element being processed in the array.
currentIndex
The index of the current element being processed in the array.
array
The array reduce was called upon.
initialValue
Optional. Value to use as the first argument to the first call of the callback.

Example:

[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, currentIndex, array) {
  return previousValue + currentValue;
});

The callback would be invoked four times, with the arguments and return values in each call being as follows:

  previousValue currentValue currentIndex arr return value
first call 0 1 1 [0, 1, 2, 3, 4] 1
second call 1 2 2 [0, 1, 2, 3, 4] 3
third call 2 3 3 [0, 1, 2, 3, 4] 6
fourth call 6 4 4 [0, 1, 2, 3, 4] 10

Array.protot

If you were to provide an initial value as the second argument to reduce, the result would look like this:

[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, currentIndex, array) {
  return previousValue + currentValue;
}, 10);
  previousValue currentValue currentIndex array return value
first call 10 0 0 [0, 1, 2, 3, 4] 10
second call 10 1 1 [0, 1, 2, 3, 4] 11
third call 11 2 2 [0, 1, 2, 3, 4] 13
fourth call 13 3 3 [0, 1, 2, 3, 4] 16
fifth call 16 4 4 [0, 1, 2, 3, 4] 20

The final call return value (20) is returned as a result of the reduce function

ype.re[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, currentIndex, array) { return previousValue + currentValue; }, 10);

Array.prototype

时间: 2024-08-14 19:43:50

01 - Execise About Array.prototype.reduce()的相关文章

Array.prototype.reduce

[Array.prototype.reduce] Array.reduce([callback, initialValue]) 参数 callback 执行数组中每个值的函数,包含四个参数: previousValue 上一次调用回调函数返回的值,或者是提供的初始值(initialValue) currentValue 数组中当前被处理的元素 currentIndex 当前被处理元素在数组中的索引, 即currentValue的索引.如果有initialValue初始值, 从0开始.如果没有从1

Using Array.prototype.reduce() to Reduce Conceptual Boilerplate for Problems on Arrays

以下内容转自FreeCodeCamp:Using Array.prototype.reduce() to Reduce Conceptual Boilerplate for Problems on Arrays Using Array.prototype.reduce() to Reduce Conceptual Boilerplate for Problems on Arrays译:使用Array.prototype.reduce()可以减少数组循环等繁琐问题 我们知道,for循环在javas

数组方法 Array.prototype

Object.prototype 数组的值是有序的集合,每一个值叫做元素,每一个元素在数组中都有数字位置编号,也就是索引,js中数组是弱类型的,数组中可以含有不同类型的元素.数组元素甚至可以是对象或者其他数组 长度范围:1====2的23方-1 new Array(100)//undifind*100 arr[5]=10; arr.length//6 push() unshift() shift() pop() var Arr=[1,true,undifind,{x:1},[1,2,3]]; A

JavaScript,通过分析Array.prototype.push重新认识Array

在阅读ECMAScript的文档的时候,有注意到它说,数组的push方法其实不仅限于在数组中使用,专门留作通用方法.难道是说,在一些类数组的地方也可以使用?而哪些是和数组非常相像的呢,大家或许一下子就可以想到就是Object对象.因为Array就是继承自Object的,可以用 [] instanceof Object,会发现返回的是true.当然大家都知道,这也不是什么新鲜事.那我们可以大胆尝试一下,如果我们将数组的push方法应用在对象上,会一个怎么样的表现呢? 我们通过call,将this的

使用Array.prototype.indexOf()的几点注意

对应indexOf这个方法,在日常开发中比较常见的应该是String.prototype.indexOf()方法,Array.prototype.indexOf()方法和其有很大的相似性,本文不想去描述其的基本用法,而是去探究在使用中需要考虑的一些问题. 一.性能 在数组元素少的情况下,我们虽然只是跳过一个元素来检索,性能微不足道,但是当我们正在处理数以千计的元素,如果使用indexOf()的第二个参数,你可能获得性能上的显著提升. 二.全等(===) indexOf方法使用全等(===)来判断

数组的方法之(Array.prototype.forEach() 方法)

forEach() 方法对数组的每个元素执行一次提供的函数. 注意: 没有返回一个新数组 并且 没有返回值! 应用场景:为一些相同的元素,绑定事件处理器! const arr = ['a', 'b', 'c']; arr.forEach(function(element) { console.log(element); }); arr.forEach( element => console.log(element)); 语法 callback为数组中每个元素执行的函数,该函数接收三个参数: cu

Array 和 Array.prototype

定义 Array 对象是用于构造数组的全局对象,数组是类似于列表的高阶对象. Array.prototype 属性表示Array构造函数的原型,并允许您向所有Array对象添加新的属性和方法. 获取相应的属性名称 Object.getOwnPropertyNames(Array) //[ "length", "name", "prototype", "isArray", "from", "of&q

将函数的实际参数转换成数组的方法,习惯用Array.prototype.slice.call(arguments)

实际参数在函数中我们可以使用 arguments 对象获得 (注:形参可通过 arguments.callee 获得),虽然 arguments 对象与数组形似,但仍不是真正意义上的数组. 我们可以通过数组的 slice 方法将 arguments 对象转换成真正的数组. 方法一:通过Array.prototype属性调用slice方法 var args = Array.prototype.slice.call(arguments); Array 本身是没有 slice 方法,它的方法在 Arr

(转)Array.prototype.slice.call自解

很多框架或者库里面都会有这句的使用,最多的还是通过Array.prototype.slice.call(arguments,0)把arguments这个伪数组转换为真正的数组.但为什么可以这么做,却一直是半懂不懂的.昨天晚上看了mootools的源码,google了一下,终于彻底明白了. call方法的作用就不用多说了,Array.prototype.slice.call(arguments,0)就类似于arguments.slice(0),但因为arguments不是真正的Array,所以它没