Skip to content

Commit

Permalink
update: 完成arrayReduce
Browse files Browse the repository at this point in the history
  • Loading branch information
HeftyKoo committed Mar 11, 2020
1 parent 664bd7e commit 35b0cc8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
* [assignValue](internal/assignValue.md)
* [baseZipObject](internal/baseZipObject.md)
* [baseSet](internal/baseSet.md)
* [arrayReduce](internal/arrayReduce.md)


* [slice](slice.md)
Expand Down
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* [assignValue](internal/assignValue.md)
* [baseZipObject](internal/baseZipObject.md)
* [baseSet](internal/baseSet.md)
* [arrayReduce](internal/arrayReduce.md)
+ [slice](slice.md)
+ [chunk](chunk.md)
+ [compact](compact.md)
Expand Down
59 changes: 59 additions & 0 deletions internal/arrayReduce.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# lodash源码分析之arrayReduce

本文为读 lodash 源码的第一百二十二篇,后续文章会更新到这个仓库中,欢迎 star:[pocket-lodash](https://github.com/yeyuqiudeng/pocket-lodash)

gitbook也会同步仓库的更新,gitbook地址:[pocket-lodash](https://www.gitbook.com/book/yeyuqiudeng/pocket-lodash/details)

## 源码分析

`arrayReduce` 的作用和数组的 `reduce` 的方法差不多。

源码如下:

```javascript
function arrayReduce(array, iteratee, accumulator, initAccum) {
let index = -1
const length = array == null ? 0 : array.length

if (initAccum && length) {
accumulator = array[++index]
}
while (++index < length) {
accumulator = iteratee(accumulator, array[index], index, array)
}
return accumulator
}
```

`iteratee``reduce` 方法一样,接收上一次计算的值 `accumulator` ,当前值,当前 `index` 和传入的数组 `array`

`accumulator` 可以用来传入初始值。

`initAccum` 用来指定是否使用数组第一个值作为初始值。

```javascript
let index = -1
const length = array == null ? 0 : array.length

if (initAccum && length) {
accumulator = array[++index]
}
```

如果 `initAccum` 为真值,并且数组不为空,则使用数组的第一个值作为初始值。

```javascript
while (++index < length) {
accumulator = iteratee(accumulator, array[index], index, array)
}
```

接下来遍历数组,调用 `iteratee` 得到计算后的结果,保存在 `accumulator` 中。

## License

[署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0)](http://creativecommons.org/licenses/by-nc-nd/4.0/)

最后,所有文章都会同步发送到微信公众号上,欢迎关注,欢迎提意见: ![](https://raw.githubusercontent.com/yeyuqiudeng/resource/master/images/qrcode_front-end-article.jpg)

作者:对角另一面

0 comments on commit 35b0cc8

Please sign in to comment.