We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
引用自 MDN:
reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值
语法为: arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
callback 接受 4 个参数
initialValue: 作为第一次调用 callback函数时的第一个参数的值, 如果没有提供初始值, 则将使用数组中的第一个元素.
const add = (a, b) => a + b [1, 2, 3, 4].reduce(add) // 10 [1, 2, 3].reduce((acc, next) => { return { ...acc, [next]: next * 5 } }, {}) // { '1': 5, '2': 10, '3': 15 }
几个注意点:
Array.prototype.myReduce = function(callback, initialValue) { let acc = initialValue let i = 0 if (typeof initialValue === 'undefined') { acc = this[0] i = 1 } for (i; i < this.length; i++) { acc = callback(acc, this[i], i, this) } return acc }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
reduce
说明
引用自 MDN:
语法为:
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
callback 接受 4 个参数
initialValue: 作为第一次调用 callback函数时的第一个参数的值, 如果没有提供初始值, 则将使用数组中的第一个元素.
例子:
实现
几个注意点:
参考
The text was updated successfully, but these errors were encountered: