...

View Full Version : Removing redundant column in arrays



calvinjose.87
09-16-2011, 11:29 AM
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??

vwphillips
09-16-2011, 02:36 PM
<!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>

calvinjose.87
10-10-2011, 07:52 AM
Thank you..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum