Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Removing redundant column in arrays

    Hi

    I read a lot of data from excel sheets into JS arrays.

    I have a situation where i have name, hours, product id and other few entries.
    I have all these details as array in array concept.

    eg. array.push(new Array(name,hours,pid,price,place))

    Redundant entries that include same names,pid, places will be found in the array.

    My prob is to sum up all the hours of the records that belong to same name and product id and store the unique records in one array and ofcourse the hours column sumed up.


    name pid hours place
    ----------------------
    aaaa 11 100 chennai
    aaaa 11 200 chennai
    cccc 22 30 coimbatore
    dddd 33 45 chennai
    dddd 22 100 chennai
    dddd 33 200 chennai

    my output should be

    name pid hours place
    ---------------------
    aaaa 11 300 chennai
    cccc 22 30 coimbatore
    dddd 33 245 chennai
    dddd 22 100 chennai

    can anyone help??

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,493
    Thanks
    3
    Thanked 499 Times in 486 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script type="text/javascript">
    /*<![CDATA[*/
    var A=[
    ['aaaa',11,100,'chennai'],
    ['aaaa',11,200,'chennai'],
    ['cccc',22,30,'coimbatore'],
    ['dddd',33,45,'chennai'],
    ['dddd',22,100,'chennai'],
    ['dddd',33,200,'chennai']
    ];
    var AA=[]
     for (var add,z0=0;z0<A.length;z0++){
      add=true;
      for (var z0a=0;z0a<AA.length;z0a++){
       if (AA[z0a][0]==A[z0][0]&&AA[z0a][1]==A[z0][1]){
        AA[z0a][2]+=A[z0][2];
        add=false;
        break;
       }
      }
      if (add){
       AA.push([A[z0][0],A[z0][1],A[z0][2],A[z0][3]]);
      }
     }
    
    alert(AA.join('\n'));
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    calvinjose.87 (10-10-2011)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you..


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •