for(var i= 0; i < foundRecords.length ; i++){
var MaleChildNew=0,
MaleNew=0,
femaleChildNew=0,
femaleNew=0,
policeMaleChildNew = 0,
policefemaleChildNew=0,
policeMaleNew=0,
policefemaleNew=0,
npoliceMaleChildNew=0,
npoliceMaleNew=0,
npolicefemaleChildNew=0,
npolicefemaleNew=0;
if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
policeMaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
policefemaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
policeMaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
policefemaleNew++;
} if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
npoliceMaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
npolicefemaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
npoliceMaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
npolicefemaleNew++;
} if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
MaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
femaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
MaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
femaleNew++;
}
}
问题是它只增加它在数据库末尾找到的类别,并返回1。我想要的是它应该增加它找到的每个条目的值并保存值。但是这个代码对于所有其他变量返回0,对于在DB末尾的那个返回1。
答案 0 :(得分:0)
您的代码将始终将变量初始化为0,因为它们位于for
循环内。因此,对于foundRecords
的每个项,变量都会重置为0。因此,在for
循环之外声明您的变量:
var MaleChildNew=0,
MaleNew=0,
femaleChildNew=0,
femaleNew=0,
policeMaleChildNew = 0,
policefemaleChildNew=0,
policeMaleNew=0,
policefemaleNew=0,
npoliceMaleChildNew=0,
npoliceMaleNew=0,
npolicefemaleChildNew=0,
npolicefemaleNew=0;
for(var i= 0; i < foundRecords.length ; i++){
if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
policeMaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
policefemaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
policeMaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
policefemaleNew++;
} if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
npoliceMaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
npolicefemaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
npoliceMaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
npolicefemaleNew++;
} if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
MaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
femaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
MaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
femaleNew++;
}
}