Counting the value of an element from array of object and store the result to new element

I would like to count the value in the element then add an element called “Count”.
Condition: count the quantity if the next element is the same “Quantity” value then add the result to new element called “Count”. I’m trying to do this using a for loop function, but my code didn’t go well.

Here my data.

let myData = [
                {
                    Name: 'Section 1', 
                    Details:[
                        { Product: 'Ballpen', Quantity: 120},
                        { Product: 'Pencil', Quantity: 120},
                        { Product: 'Marker', Quantity: 80},
                        { Product: 'Highlighter', Quantity: 120}
                    ]
                },
                {
                    Name: 'Section 2', 
                    Details:[
                        { Product: 'Paper', Quantity: 40},
                        { Product: 'Bond Paper', Quantity: 75},
                        { Product: 'Colored Paper', Quantity: 75},
                        { Product: 'Oslo paper', Quantity: 40}
                    ],
                }
            ]

Expected Output:
My table will looks like this…


  • PRODUCT * Quantity * Count *

  • Ballpen * 120 * 2 *
  • Pencil * 120 * 2 *
  • Marker * 80 * 1 *
  • Highlighter * 120 * 1 *

Meaning it will count every time the quantity value of the product will be the same value on the next element. Stop the counting even if there is still same value from other element but not the next element.

※ On the Console.log(). It will looks like…

Section 1 : {
                [ Product = 'Ballpen', Quantity = 120 , Count = 2],
                [ Product = 'Pencil', Quantity = 120 , Count = 2],
                [ Product = 'Marker', Quantity = 80 , Count = 1],
                [ Product = 'Highlighter', Quantity = 120 , Count = 1],
            },
Section 2 : {
                [ Product = 'Paper', Quantity = 40 , Count = 1],
                [ Product = 'Bond Paper', Quantity = 75 , Count = 2],
                [ Product = 'Colored Paper', Quantity = 75 , Count = 2],
                [ Product = 'Oslo paper', Quantity = 40 , Count = 1],
            },
//Other codes here...

I’m sorry, but i’m still learning on how to manipulate data, so thank you for your understanding. 🙂