0
如果不存在,我有以下函數應該爲數組添加值。如果不存在,jquery將值添加到數組中
var category_json = new Array();
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>Reports/appointment_distribution_by_group/",
dataType: 'JSON',
data: {county: county, sub_county: sub_county, facility: facility, date_from: date_from, date_to: date_to},
success: function (data) {
// Populate series
for (i = 0; i < data.length; i++) {
/* Values returned from variable data as data[i].group_name = > Adolescents,Adolescents,All,All,ART Clients,ART Clients,ART Clients,ART Clients,Lactating woman,New Clients,New Clients,Paeds on ART,Paeds on ART,PMTCT,PMTCT */
if(jQuery.inArray(data[i].group_name,category_json)!== -1) {
//element exists in array
console.log(" Found ..." + data[i].group_name);
} else {
//element not found in array
category_json.push([data[i].group_name]);
console.log("Not Found ..."+data[i].group_name);
console.log(category_json);
}
}
var type = jQuery.type(category_json);
alert(category_json);
alert(type);
// draw chart
}, error: function (errorThrown) {
}});
我想將返回的值添加到類別json數組,但只有唯一值。但是,檢查數組中的值並不會返回true。 應該使用哪種條件來檢查數組中是否存在該值?
的[我如何檢查如果一個數組包含在JavaScript對象?(可能的複製https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) –