Skip to content
Advertisement

Dividing up items based on quantity

We have a custom dispatch system where we have orders, if the total quantity within the order exceeds 2 then we need to split the contents over two orders. So for instance…

Order contains:

  • Item A x2
  • Item B x2

I need to move Item B to a second order.

Another example would be:

  • Item A x4

I would need to move 2 items to a second order so I’m left with Item A x2 in one output and the same in another output.

I’m using the following to loop through the items and quantities within an order.

JavaScript

How can I total up the items in that order – as soon as any of the quantities hit 2, can I move those to a second array? Is using multiple arrays the best approach for this?

This is an example of an array that’s been generated (this can be variable):

JavaScript

Advertisement

Answer

Managed to figure this out by using the following, this gives me the separate parcels I needed:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement