Home  >  Q&A  >  body text

javascript - What does the 0 in this js code mean?

What does the 0 in this js code mean?
In the red box in the screenshot below:

(Sorry if the question is stupid)

大家讲道理大家讲道理2671 days ago1100

reply all(5)I'll reply

  • phpcn_u1582

    phpcn_u15822017-06-28 09:25:12

    .Reduce is a circular array of values. Start by passing the first item of the array to the parameter res, and the second item to cur. Next time, pass the result of your first calculation to res, and pass the third item of the array to Cur, perform calculation again...until the array is looped, and return the final calculation result.
    .The reduce mechanism is like this, but since the first item of your array is an object and cannot be calculated, you need to pass 0 to res at the beginning, and pass the first item of the array to cur. In this way, the sum of price can be calculated . The second parameter of .reduce does this, which is equivalent to the first item of the array becoming 0, and the second item is the object you set

    reply
    0
  • typecho

    typecho2017-06-28 09:25:12

    Excerpted from MDN:
    https://developer.mozilla.org...

    arr.reduce(callback,[initialValue])

    initialValue

    Optional, whose value is used as the first parameter of the first callback.

    reply
    0
  • 阿神

    阿神2017-06-28 09:25:12

    The value of res when reduce is executed for the first time. This problem can be solved by just checking the API

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-28 09:25:12

    The initial value of the

    reduce method.

    Please refer to: Array.prototype.reduce()

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-28 09:25:12

    Initial value, PHP’s array_reduce function also looks like this

    reply
    0
  • Cancelreply