MikeMcl/bignumber.js

since bignumber.js is immutable then how to get the sum of an bignumber array?

laptopmutia opened this issue · 1 comments

since bignumber.js is immutable then how to get the sum of an bignumber array?

is it fine if I map my array and chain it by .plus(Bignumber(number)).plus(Bignumber(number))

You can use reduce:

const arr = [
  BigNumber(0),
  BigNumber(1),
  BigNumber(2),
  BigNumber(3),
  BigNumber(4),
  BigNumber(5),
  BigNumber(6),
]

const sum = arr.reduce((acc, next) => {
  return acc.plus(next)
}, BigNumber(0))

console.log(sum.toString())
// 21