Pricing Blog

How to use the Reduce formula?

  • sanedealer-1383104379996995664

    Edwin Paul

    2 months ago

    Trying to wrap my head around the Reduce formula in NC. I think I’d get it better if someone could walk me through a use case.
    1383104380831797348-image.png
  • whitep4nth3r-1383124716440649801

    salma

    2 months ago

    What are you trying to do?

    I find most things can be achieved without using reduce to make things simpler. It’s not an easy function to understand in JavaScript.
  • sanedealer-1383167432256131182

    Edwin Paul

    2 months ago

    Well, i'm not trying to do anything particular with the reduce formula. I just wanna understand it. If you could provide an example use case, that would help a lot.
  • whitep4nth3r-1383178925538283571

    salma

    2 months ago

    I can do next week ☺️
  • whitep4nth3r-1383179351717187604

    salma

    2 months ago

    In the meantime this post has some practical examples

    https://thecodebarbarian.com/javascript-reduce-in-5-examples.html
  • This is the key benefit

    “The reduce() method works in a similar manner to the forEach() method, with the added ability to collect the result of each iteration as a single value.
  • whitep4nth3r-1383179904555942143

    salma

    2 months ago

    This is heavier reading but is very extensive

    https://www.stephanmiller.com/javascript-reduce/
  • sanedealer-1383190868642758747

    Edwin Paul

    2 months ago

    appreciate it!
  • whitep4nth3r-1384177674410393611

    salma

    2 months ago

    Here's a quick reduce usage example to show using the formula to sum up the total property of a list of objects in an array:

    1. the formula in the reduce is summing the item total, and the result, which is the result of the accumulator each time the reducer loops
    2. The accumulator starts at 0, as we start with 0 items until we go around in a loop to sum all the items up

    https://editor.nordcraft.com/projects/reduce_example/branches/start/components/HomePage?canvas-width=800&canvas-height=800&selection=formulas.ojBjiX&rightpanel=style
  • sanedealer-1384623425041203220

    Edwin Paul

    2 months ago

    Thank you for getting back to me, Salma. I was wondering if there's a unique use case that only the Reduce formula can handle?
    1384623424764117153-image.png
  • sanedealer-1384623679387734249

    Edwin Paul

    2 months ago

    Or is it in anyway more optimized for such tasks?
  • keddedev-1384639762320855071

    Kedde

    2 months ago

    I'm one of those devs that love the reduce function as it's quite powerful and I use it quite a lot.
    It takes an array of type T and turns it into any type Y. (T[]) => Y.

    It's quite useful if you want to turn an array into an object for example. That's what I use it for mostly. One could argue that map+fromEntries could do the same. It's a matter of preference.

    All the other array functions like map and filter and join etc. can be written using reduce.

    A side note: in some programming languages reduce is called fold or foldLeft.
  • keddedev-1384641322232582275

    Kedde

    2 months ago

    Reduce can also reduce (heh) the number of iterations of an array. If you want to change the type of the array and the size of it without reduce you would need to loop it twice. Once with map and once with filter. Reduce allows you to get the same result in just one iteration. It can be useful for large datasets.
  • sanedealer-1384829864913145980

    Edwin Paul

    2 months ago

    Thanks for sharing your insights! It's always cool to hear how other devs wield their favorite tools.
  • whitep4nth3r-1384906393307451506

    salma

    2 months ago

    yeah that's what I was saying at the start really, you can usually do what you need to do without reduce 😅 but I think in some circumstances it can make things quicker, as Kedde says, and prevents too much looping in some cases
  • lucasg-1384906679530946601

    Lucas G

    2 months ago

    Reduce is powerful and it’s the reason why you most likely won’t ever need things like traditional loops.
    It’s probably worth noting that accumulator doesn’t have to be a number either.
    It can be an object for example

    Reduce formula does a lot