如何使用jQuery从数组中检索值以从其他数组中获取值?
我有几个数组:如何使用jQuery从数组中检索值以从其他数组中获取值?,jquery,arrays,Jquery,Arrays,我有几个数组: cl = ["Chile", "15", "83"]; ar = ["Argentinia", "16.5", "90"]; py = ["Paraguay", "19", "81.5"]; route = ["cl;ar", "ar;py"]; 是否可以在一个数组中循环并从其他数组中获取特定值? 我试过这个,但没用: $.each(route, function(index,value) { place = v.split(';'); start = pla
cl = ["Chile", "15", "83"];
ar = ["Argentinia", "16.5", "90"];
py = ["Paraguay", "19", "81.5"];
route = ["cl;ar", "ar;py"];
是否可以在一个数组中循环并从其他数组中获取特定值?
我试过这个,但没用:
$.each(route, function(index,value) {
place = v.split(';');
start = place[0];
end = place[1];
console.log('from '+start[0]+' to '+end[0]);
});
日志应显示:“从智利到阿根廷”,“从阿根廷到巴拉圭”
但它只写“从c到a”,“从a到p”
我做错了什么,如何从其他数组读取值?您可以使用哈希表:
var countries = {
cl: {name: 'Chile', prop1: 15, prop2: 83},
ar: {name: 'Argentinia', prop1: 16.5, prop2: 90},
py: {name: 'Paraguay', prop1: 19, prop2: 81.5}
}
然后,您可以在需要时查找:
$.each(route, function(index,value) {
place = value.split(';');
start = place[0];
end = place[1];
console.log('from '+ countries[start].name + ' to ' + countries[end].name);
});
尝试使用哈希表:
var hashtable = {};
hashtable['cl'] = ["Chile", "15", "83"];
hashtable['ar'] = ["Argentinia", "16.5", "90"];
hashtable['py'] = ["Paraguay", "19", "81.5"];
route = ["cl;ar", "ar;py"];
$.each(route, function(index,value) {
var fromTo = value.split(";");
alert('from '+hashtable[fromTo[0]][0]+' to '+hashtable[fromTo[1]][0]);
});
如果可能的话,我会将问题中的数组中的其他值移动到您的
国家
对象的属性中…@ShaneBlake不想编造随机键名。不过,这一点很好,我已经把它们搬进来了。很多时候,当解决方案难以捉摸时,我发现我的数据模型设计得很糟糕……谢谢马特!eval(…)工作得很好。