I want to push all 0.0 value in object, so I can count how many 0.0 value in an object. So far I’ve made a code to push all values (including 0.0) but now I just want to push ONLY 0.0 VALUE.
for example:
in ['cm_per1'] there is 2 “0.0”, then I want to push them as final_results['ESL']['cm_per1'] and when I call final_results['ESL']['cm_per1'].length, it will show “2” (because there is 2 “0.0” in cm_per1)
here is what I’ve made so far >> http://jsfiddle.net/xKJn8/26/
var data = {
"MyData": [
{
"cm_per1": "21.9",
"cm_per2": "31.8",
"tipe": "ESL"
},
{
"cm_per1": "8.6",
"cm_per2": "7.0",
"tipe": "ESL"
},
{
"cm_per1": "3.2",
"cm_per2": "0.0",
"tipe": "ESL"
},
{
"cm_per1": "0.0",
"cm_per2": "0.0",
"tipe": "ESL"
},
{
"cm_per1": "0.0",
"cm_per2": "0.0",
"tipe": "ESL"
}
]
};
var final_results = {},
type,
current_row= "";
for (var i=0; i<data.MyData.length; i++) {
current_row = data.MyData[i];
type = current_row.tipe;
//I want to count how many cm_per1 and cm_per2 that have 0.0 value
if (!final_results[type]) {
final_results[type] = {
"cm_per1": [],
"cm_per2": []
};
}
final_results[type].cm_per2.push(current_row.cm_per2);
final_results[type].cm_per1.push(current_row.cm_per1);
}
//but the result is it counts all cm_per1 and cm_per2, and what I need is only counts that have 0.0 value
console.log(final_results['ESL']['cm_per1'].length);
1 Answer