Reputation: 33
I have an array, I have loop into the array and find the values which are having values '100,0 or rest' in a row.
var num100=100,num0=0,rest,count100=0,count0=0,countrest=0;
var array1= [
/* Reduced data set */
[ "Manoj", 100, 1, 1, 1 ],
[ "Jai", 0, 0, 0, 0 ],
[ "Pradeep", 1, 1, 1, 1 ],
[ "Reena", 0, 0, 0, 0 ],
[ "Santos", 100, 1, 1, 1 ],
[ "Brock", 0, 1, 1, 1]
];
for(var i=0;i<array1.length;i++) {
for(var j=1;j<array1[i][j].length;j++) {
if(array1[i][j]==100){count100++;}
else if(array1[i][j]==0){count0++;}
else countrest++;
}
}
What I want is, I want to loop into each rows, if a particular row that has values like zero,hundred or rest, then incrementation should take place as follows.
[ "Jai", 0, 0, 0, 0 ]
then count the count0++
, that is increment by one.
if
[ "Jai", 0, 1, 0, 1 ]
then count the countrest++
, that is increment by one or
else
[ "Jai", 100, 100, 100, 100 ]
then count the count100++
, that is increment by one
So, how to get this done. It should only see if a row has only 100 value if only a row is 100 then only increment...
Upvotes: 0
Views: 48
Reputation: 5994
If the array structure is always the same
for(var i=0;i<array1.length;i++){
if(array1[i][1] == array1[i][2] == array1[i][3] == array1[i][4]){
if(array1[i][1]==100){count100++;}
else if(array1[i][1]==0){count0++;}
}else{
countrest++;
}
}
Upvotes: 0
Reputation: 1131
for(key in array1){
if(array1[key].indexOf(100)!=-1){
count100++;
}
else if(array1[key].indexOf(1)!=1){
countrest++;
}
else{
count0++;
}
}
Upvotes: 0
Reputation: 3148
This should do the trick. For each subArray you want to test a few variables are set to true. If it turns out that one of the items is not 0 or not 100, the accompanying variable is set to false. The results are catched and interpreted afterwards.
var count0 = 0; var count100 = 0; var countrest = 0;
for(var a = 0 ; a < array1.length ; a++){
var zero = true; var hundred = true;
for(var b = 1 ; b < array1[a].length ; b++){
if(array1[a][b] != 0){zero = false;}
else if(array1[a][b] != 100){hundred = false;}
}
if(zero == true){count0++;}
else if(hundred == true){count100++;}
else{countrest++;}
}
Upvotes: 1