DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 968,547 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Jastria Rahmat
Jastria Rahmat

Posted on • Updated on

Nested Array reduce function.

Given these:

const cart = [
    {
      products: [
        { price: 65 },
        { price: 30 }
      ]
    },
    {
      products: [
        { price: 15 },
        { price: 25 } 
      ]
    }
]
Enter fullscreen mode Exit fullscreen mode

Solution:

const totalPrice = cart.reduce((accumulator, item) => {
  const productSum = item.products.reduce((productAcc, product) => {
    return productAcc + product.price;
  }, 0);
  return accumulator + productSum;
}, 0);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

In defense of the modern web

I expect I'll annoy everyone with this post: the anti-JavaScript crusaders, justly aghast at how much of the stuff we slather onto modern websites; the people arguing the web is a broken platform for interactive applications anyway and we should start over;

React users; the old guard with their artisanal JS and hand authored HTML; and Tom MacWright, someone I've admired from afar since I first became aware of his work on Mapbox many years ago. But I guess that's the price of having opinions.