[Vuejs]-Counting the value of an element from array of object and store the result to new element

1๐Ÿ‘

โœ…

I hope this is what you are looking for

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}
                    ],
                }
            ]
            myData.forEach((ele, idx)=>{
                ele.Details.forEach((ele2, idx2)=>{
                    let value = ele2.Quantity;
                    let count = 0;
                    let sliced = ele.Details.slice(idx2, ele.Details.length)
                    let ok = true;
                    sliced.forEach(ele3=>{
                        if (ele3.Quantity == value && ok)
                            count++;
                        else
                            ok = false
                    })

                    ele2.Count = count;
                    if(typeof ele.Details[idx2 - 1] != 'undefined')
                    {
                        if (ele.Details[idx2].Quantity == ele.Details[idx2 - 1].Quantity)
                            ele.Details[idx2].Count = ele.Details[idx2 - 1].Count
                    }
                    
                })
            })
            console.log(myData);
๐Ÿ‘คSalah a

Leave a comment