+ 1
Objects in an array with reduce
Why console.log's value always NaN https://code.sololearn.com/Ws3C0N7dOdsa/?ref=app
1 Réponse
+ 1
If you use reduce without intialValue (second parameter), reduce would take the first element of the array as initialValue of accumulator, which is
{
nama:"Bambang",
kelas:"12 RPL 1",
nilai:50
}
So the the output would be Not a Number (NaN).
If you want to calculate sum of object nilai, you should set initialValue to zero.
angka = aObj.reduce((now,than)=> now + than["nilai"], 0);