.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
.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
Excerpted from MDN:
https://developer.mozilla.org...
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
reduce
method.Please refer to: Array.prototype.reduce()
Initial value, PHP’s array_reduce function also looks like this