implement Array.prototype.reduce()
2022年5月1日小于 1 分钟
implement Array.prototype.reduce()
Question
Array.prototype.reduce() is a handy method to process arrays.
Here is a simple task - Could you implement it by yourself?
[1,2,3].myReduce((sum, item) => sum + item)
// 6
- do not use native Array.prototype.reduce() in your code
- your function is only tested against valid array (no array-like objects)
- thanks to pajadev for suggesting this
Code
Array.prototype.myReduce = function () {
// your code here
}