Reduce array of nested objects?

Hey guys, got this array of objects that I want to reduce the quantity of each item to a single summed up value. What is the most efficient way of achieving this? cartItems.reduce() is definitely what I’m wanting, but how would I be able to destructure this in the parameters? The key lives at precisely cartItems[<index>].node.quantity
Screen Shot 2021-01-19 at 3.00.49 PM

Please provide a copy of the original object and a sample of what you would want it to look like. Please don’t take pictures of code - cut and paste it, just format it by putting 3 backticks on the line before and the same on the line after.


It’s really hard to tell from that small screenshot. Do you have some actual code?

1 Like

If you’re trying to turn an array into an array with different contents, you want .map(). If you want to turn an array into a single value (including a single object), you want .reduce(). These can be combined in a number of convoluted ways.


Yeah, I’m just trying to reduce the array to the quantity from all of the objects into a number. I don’t know what code I could possibly post that @kevinSmith is asking for lol. It’s an array of objects that is at cartItems[<index>].node.quantity

What have you tried so far? We aren’t going to write code for you, but we can help you improve your code.

I know it’s reduce I’m looking for. You really can’t shoot me a one liner that would grab that value?

We really don’t do that here. But we’re happy to help you develop one. Have you used .reduce() before?

1 Like

I know, I’ve been here a long time now. Nevermind buddy

Sure thing pal. Just let us know if you have any questions while you are writing your .reduce().

It looks like you should be able to get the quantity from inside your reduce with currentItem.node.quantity

Is that what you were after?

Just to reduce each object down to one number value.

Can you show me your reduce function so far? The snippet I shared will get you the value you’re after once you’re inside the reduce

1 Like

I wasn’t sure how to dig into the objects within the reduce.

Oh I see

Did you try cartItems.reduce()

Maybe I’m not understanding the structure of the object but it just looks like an array of objects with a single node and quantity unless there are more nodes on each object?

I was mainly looking for the actual object. We could easily copy and test it that way. A clearer picture of what you want to end up with would be nice too.

It sounds to me like you want to end up with

  { quatity: 2 },
  { quanity: 14 },
  // ...

Is that right? If that’s the case, the map would be what you want.

But really, the more information you can provide the better we can answer. But as said, we’re really more about guiding people that doing the work for other people.


Reduce iterates over an array. You can treat each element in the array the say way that you would in a loop.

My suggestion is to figure out your logic first, using the tools that you are comfortable with (such as for loops) and get it working. Then you can tighten it up by refactoring it into higher order functions.


Thanks, I didn’t think this would be such a damn burden on everyone lol.

Oh, it is no burden at all. We are happy to help you improve your work on solving this problem.

1 Like