javascript - What does the 0 in this js code mean?
大家讲道理
大家讲道理 2017-06-28 09:23:12
0
5
1061

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

(Sorry if the question is stupid)

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all (5)
phpcn_u1582

.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

    typecho

    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.

      阿神

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

        大家讲道理

        The initial value of the

        reducemethod.

        Please refer to: Array.prototype.reduce()

          phpcn_u1582

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

            Latest Downloads
            More>
            Web Effects
            Website Source Code
            Website Materials
            Front End Template
            About us Disclaimer Sitemap
            php.cn:Public welfare online PHP training,Help PHP learners grow quickly!