我想添加两个来自length()
函数的值。我有这个代码段http://jsfiddle.net/uERXG/
这是代码
$(document).ready(function(){
var data = [{"water_pay": "no", "respondent": "community", "research_asst_name": "Haruna Mohammed", "water_used_season": "year_round", "_bamboo_dataset_id": "", "_deleted_at": null, "water_point_condition": "functioning", "_xform_id_string": "_08_Water_points_CV", "other_point_1km": "no", "_attachments": ["north_ghana/attachments/1351696546452.jpg"], "communities_villages": "Gumaryili", "end": "2012-11-12T11:46:32.454Z", "animal_number": "more_500", "water_point_id": "xxx", "start": "2012-10-31T15:11:04.618Z", "water_connected": "no", "water_manager_name": "community members", "_status": "submitted_via_web", "enum_id_1": "5", "water_lift_mechanism": "no", "districts_divisions": "northern", "_uuid": "f8bcee72d7a0400fb99ae11bbf804010", "grid": "grid_further_500_m", "date": "2012-10-31", "formhub/uuid": "4d41d54d134c4bfa9078571addd819b9", "road_available": "no", "water_functioning": "yes", "_submission_time": "2012-11-13T07:13:57", "signal": "low", "water_source_type": "dam_dugout", "_geolocation": ["10.1892764", "-0.66410362"], "water_point_image": "1351696546452.jpg", "water_point_geocode": "10.1892764 -0.66410362 155.10000610351563 5.0", "deviceid": "355047040123780", "locations_wards": "west_mamprusi", "water_manager": "community", "water_developer": "community", "_id": 381705, "animal_point": "yes"}, {"water_pay": "no", "respondent": "community", "research_asst_name": "Haruna Mohmmed", "water_used_season": "year_round", "_xform_id_string": "_08_Water_points_CV", "_bamboo_dataset_id": "", "_deleted_at": null, "water_point_condition": "functioning", "water_mechanism_plate": "no", "other_point_1km": "yes", "_attachments": ["north_ghana/attachments/1351701849971.jpg"], "water_lift_mechanism_type": "manual_power", "water_point_image": "1351701849971.jpg", "end": "2012-11-12T11:49:36.619Z", "animal_number": "50_to_500", "water_point_id": "xxx", "start": "2012-10-31T16:41:49.738Z", "water_connected": "no", "water_manager_name": "Amadu Salifu", "_status": "submitted_via_web", "enum_id_1": "5", "water_lift_mechanism": "yes", "districts_divisions": "northern", "_uuid": "c2f6b298955f47ab9f177bee1214141d", "road_type": "gravel", "grid": "grid_further_500_m", "date": "2012-10-31", "communities_villages": "Selinvoya", "formhub/uuid": "4d41d54d134c4bfa9078571addd819b9", "road_available": "yes", "water_functioning": "yes", "_submission_time": "2012-11-13T07:14:04", "signal": "high", "water_source_type": "unprotected_well", "_geolocation": ["10.28173052", "-0.56901122"], "water_point_geocode": "10.28173052 -0.56901122 201.89999389648438 5.0", "deviceid": "355047040123780", "locations_wards": "west_mamprusi", "water_manager": "individual", "water_developer": "community", "_id": 381706, "animal_point": "yes"}, {"water_pay": "no", "respondent": "community", "research_asst_name": "Haruna Mohammed", "water_used_season": "year_round", "_xform_id_string": "_08_Water_points_CV", "_bamboo_dataset_id": "", "_deleted_at": null, "water_point_condition": "functioning", "water_mechanism_plate": "no", "other_point_1km": "yes", "_attachments": ["north_ghana/attachments/1351702462336.jpg"], "water_lift_mechanism_type": "manual_power", "water_point_image": "1351702462336.jpg", "end": "2012-10-31T16:57:37.864Z", "animal_number": "50_to_500", "water_point_id": "xxx", "start": "2012-10-31T16:52:02.601Z", "water_connected": "no", "water_manager_name": "Sulemana Abdulai", "_status": "submitted_via_web", "enum_id_1": "5", "water_lift_mechanism": "yes", "districts_divisions": "northern", "_uuid": "6bc6d188611d47f6a666cfd1eaa33998", "road_type": "paved", "grid": "grid_further_500_m", "date": "2012-10-31", "communities_villages": "Selinvoya", "formhub/uuid": "4d41d54d134c4bfa9078571addd819b9", "road_available": "yes", "water_functioning": "yes", "_submission_time": "2012-11-13T07:14:07", "signal": "high", "water_source_type": "borehole", "_geolocation": ["10.28169238", "-0.56962993"], "water_point_geocode": "10.28169238 -0.56962993 202.60000610351563 5.0", "deviceid": "355047040123780", "locations_wards": "west_mamprusi", "water_manager": "community", "water_developer": "individual", "_id": 381707, "animal_point": "no"}]
$.each(data,function(k,v){
if(v['water_point_condition'] === 'functioning'){
console.log($(data).find(v['water_point_condition'] === 'functioning').length);
}
});
$.each(data,function(key,value){
if(value['animal_point'] === 'yes'){
var one = $(data).find(value['animal_point'] === 'yes').length;
}
if(value['water_point_condition'] === 'functioning'){
var two = $(data).find(value['water_point_condition'] === 'functioning').length;
}
console.log(parseFloat(one) + parseFloat(two));
});
});
当我运行NaN
console.log(parseFloat(one) + parseFloat(two));
我该如何为我添加两个长度?
答案 0 :(得分:0)
这是因为当animal_point为" no"时,其中一个值未定义。将你的功能改为下面然后NaN不会来
$.each(data,function(key,value){
var one = 0; var two = 0
if(value['animal_point'] === 'yes'){
one = $(data).find(value['animal_point'] === 'yes').length;
}
if(value['water_point_condition'] === 'functioning'){
two = $(data).find(value['water_point_condition'] === 'functioning').length;
}
console.log(parseFloat(one) + parseFloat(two));
});