+ 1

Objects in an array with reduce

Why console.log's value always NaN https://code.sololearn.com/Ws3C0N7dOdsa/?ref=app

1st Mar 2020, 1:06 AM
Hafizd Jubaidir
Hafizd Jubaidir - avatar
1 Resposta
+ 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);
1st Mar 2020, 2:13 AM
Calviղ
Calviղ - avatar