Arrays 使用.each访问jquery中的JSON数组对象
我有一系列JSON数组,每个数组中都有对象,其中一个数组是在.change函数中选择的,数组在if语句中 var国家/地区数据Arrays 使用.each访问jquery中的JSON数组对象,arrays,json,object,Arrays,Json,Object,我有一系列JSON数组,每个数组中都有对象,其中一个数组是在.change函数中选择的,数组在if语句中 var国家/地区数据 $("#job").change(function () { selectedValue = $("#job option:selected").val(); if (selectedValue == "job_1") { country_data = [{ "country_id": 1,
$("#job").change(function () {
selectedValue = $("#job option:selected").val();
if (selectedValue == "job_1") {
country_data = [{
"country_id": 1,
"country": "Luxembourg",
"local_wage": "407",
"wage": "489",
"exchange": "0.98"
}, {
"country_id": 2,
"country": "Norway",
"local_wage": "3200",
"wage": "378",
"exchange": "9.57"
}, {
"country_id": 3,
"country": "Austria",
"local_wage": "290",
"wage": "337",
"exchange": "0.87"
}];
} else if (selectedValue == "job_2") {
country_data = [{
"country_id": 1,
"country": "Luxembourg",
"local_wage": "874",
"wage": "654",
"exchange": "0.24"
}, {
"country_id": 2,
"country": "Norway",
"local_wage": "741",
"wage": "365",
"exchange": "4.77"
}, {
"country_id": 3,
"country": "Austria",
"local_wage": "854",
"wage": "634",
"exchange": "0.43"
}];
} else if (selectedValue == "job_3") {
country_data = [{
"country_id": 1,
"country": "Luxembourg",
"local_wage": "854",
"wage": "985",
"exchange": "0.25"
}, {
"country_id": 2,
"country": "Norway",
"local_wage": "645",
"wage": "874",
"exchange": "5.55"
}, {
"country_id": 3,
"country": "Austria",
"local_wage": "201",
"wage": "256",
"exchange": "0.78"
}];
}
}).change();
我还有一个函数,它使用.each来访问所选的数组数据,但是我似乎无法让它访问所选JSON数组中的数据
//variable country_data below wont access the selected array
$.each(country_data, function(i) {
var regEx = new RegExp('^' + searchStr + '\\w*\\b','i');
if (country_data[i].country.match(regEx) ||
(i == 3 && searchStr.toLowerCase() == "us") ||
(i == 4 && ((searchStr.toLowerCase() == "uk") ||
("Great Britain".match(regEx)) ||
("Britain".match(regEx)) ||
("England".match(regEx)) ||
("Wales".match(regEx)) ||
("Scotland".match(regEx)) ||
("Northern Ireland".match(regEx))
)
)
)
$suggestionList.append('<li class="country_result" role="option"><a href="#'+country_data[i].country_id+'">' + country_data[i].country + '</a></li>');
if (country_data[i].country.toLowerCase() === searchStr.toLowerCase()) {
exactMatch = true;
select_current_country(i+1);
return;
}
});
//下面的变量国家/地区\u数据无法访问所选阵列
美元。每个(国家/地区数据,功能(i){
var regEx=new RegExp('^'+searchStr+'\\w*\\b','i');
if(country_data[i].country.match(regEx)||
(i==3&&searchStr.toLowerCase()||
(i==4&((searchStr.toLowerCase()==uk)||
(“大不列颠”。匹配(正则表达式))||
(“英国”。匹配(regEx))||
(“英格兰”。比赛(regEx))||
(“威尔士”。匹配(正则表达式))||
(“苏格兰”。比赛(regEx))||
(“北爱尔兰”。匹配(regEx))
)
)
)
$suggestionList.append(“”);
if(country_data[i].country.toLowerCase()==searchStr.toLowerCase()){
精确匹配=正确;
选择当前国家(i+1);
回来
}
});
我收到错误“无法读取未定义的”属性“长度”。如何修改.each函数,以便访问对象数据?非常感谢您的帮助!谢谢,因为当脚本第一次运行时,变量country_数据将未定义。 不要使用ifelse-使用开关