You must Sign In to post a response.
  • Category: JavaScript

    Group by & sum in jQuery, javascript

    there are some values in below format

    [[A,150,500,6000],[B,160,700,7000],[B,200,633,9000],[B,210,633,9000]]

    i need to do group by & do sum

    i need final output to be

    A,150,500,6000
    B,570,1966,2500

    the above need to be achieved either with java script or j query
  • #760718
    Hello Anbu,

    In which language you are doing this and what it is?

    Can you elaborate it..?? So that we can help you better.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."

  • #760723
    Hi
    anbu

    You can go through following url


    http://stackoverflow.com/questions/8550183/sum-of-values-in-an-array-using-jquery

    http://stackoverflow.com/questions/19220971/input-array-sum-with-jquery

    http://stackoverflow.com/questions/9627475/sum-values-of-a-javascript-array


    Hope it will be helpful to you

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #764169
    Hi anbu,

    try following:

    var ArrInput = [["A", 10], ["B", 20], ["B", 30], ["C", 40], ["A", 30]];
    var ArrItems = {}, base, key;
    for (var i = 0; i < ArrInput.length; i++)
    {
    base = ArrInput[i];
    key = base[0];
    if (!ArrItems[key]) // if not already present in the map, add a zero item in the map
    {
    ArrItems[key] = 0;
    }
    ArrItems[key] += base[1];// add new item to the map entry
    }

    var ArrOutput = [], tempList; // Generate final array
    for (key in ArrItems)
    {
    tempList = [key, ArrItems[key]]// create array entry for the map value
    ArrOutput.push(tempList);// put array entry into the output array
    }
    document.body.innerHTML = JSON.stringify(ArrOutput);

    Hope this will help you.

    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com


  • Sign In to post your comments