The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Calculate a sum using reduce in ImmutableJS

Calculate a sum using reduce in ImmutableJS

1:22
The reduce function in ImmutableJS allows you to transform and create new data from an existing data structure. In this example, we will do some math and calculate a sum based on data in an ImmutableJS Map.
Watch this lesson now
Avatar
egghead.io

The reduce function in ImmutableJS allows you to transform and create new data from an existing data structure. In this example, we will do some math and calculate a sum based on data in an ImmutableJS Map.

Avatar
Taylor Bell

In this example, I'm returning an assignment with += :

const subtotal = cart.get('items').reduce((t, i) => t += i.get('price'), 0)

This code works, but it's not necessary to do the assignment, since the sum is what I actually want:
const subtotal = cart.get('items').reduce((t, i) => t + i.get('price'), 0)

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?