javascript中initialValue的工作原理是如何减少功能? [英] how does initialValue work in javascript reduce function?

查看:44
本文介绍了javascript中initialValue的工作原理是如何减少功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定我读错了,但是MDN这样说...

I'm sure I'm reading this wrong but MDN says this...

初始值
用作首次调用回调的第一个参数的值.如果未提供初始值,则将使用数组中的第一个元素.在没有初始值的空数组上调用reduce()是错误的.

initialValue
Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used. Calling reduce() on an empty array without an initial value is an error.

然后 说这 ...这些不是说不同的话吗?谢谢!

then says this...Aren't these saying different things? Thanks!

如果未提供initialValue,则reduce()将执行从索引1开始的回调函数,并跳过第一个索引.如果提供initialValue,它将从索引0开始.

If initialValue isn't provided, reduce() will execute the callback function starting at index 1, skipping the first index. If initialValue is provided, it will start at index 0.

推荐答案

我的措辞有点令人困惑.

The wording is a bit confusing, I agree.

将这两个语句放在一起:

Putting the two statements together:

如果未提供初始值,则数组中的第一个元素将被使用

If no initial value is supplied, the first element in the array will be used

如果未提供initialValue,则reduce()将执行回调从索引1开始的功能,跳过第一个索引

If initialValue isn't provided, reduce() will execute the callback function starting at index 1, skipping the first index

这两个语句实际上描述了在不提供初始值的情况下reduce操作的两个不同特征:

These two statements are actually describing two different characteristics of the reduce operation when no initial value is provided:

  1. 用作初始值的默认值
  2. 该操作将使用的起始数组索引

这种措辞更有意义吗?:

Does this wording make more sense?:

如果未提供初始值,则第一个元素将用作初始值.在这种情况下,回调函数将从索引1开始,因为已经通过将索引0的值用作默认起始值​​来考虑了索引0.

If no initial value is provided, the first element will be used as the initial value. In this case, the callback function will start at index 1, since index 0 has already been accounted for by using it's value as the default starting value.

这篇关于javascript中initialValue的工作原理是如何减少功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆