I am attempting to calculate the value in each element and then create a new element called "Count". Rule: Count the quantity if the next element has the same "Quantity" value, then add the total to a new element named "Count". I have tried implementing this using a for loop function, but unfortunately, my code did not work as expected.
Here is the data I am working with:
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 Result: The table will look like this...
- PRODUCT * Quantity * Count *
- Ballpen * 120 * 2 *
- Pencil * 120 * 2 *
- Marker * 80 * 1 *
- Highlighter * 120 * 1 *
This means that it will count every time the quantity of a product matches the quantity of the next element. The counting stops even if there are other elements with the same value, but they are not consecutive.
※ On the Console.log(). It will appear as...
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 apologize for any confusion, as I am still learning how to manipulate data. Thank you for your patience and understanding. :)