我从php关联数组到jquery对象获得键和值:
$names = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
$js = json_encode($names);
echo 'var names= ' . $js . ";";现在,我知道了如何遍历所有的名称并获得它们的键和值:
$.each(names, function(key, value){
alert(key + value);
});但我只需要具体的价值。例如,我如何才能只得到"Ben“和"37”而不遍历所有的名称?
发布于 2016-01-15 16:29:10
您可以使用Object.keys来完成这个任务。
var index = 1; // the nth key you want to fetch, 1 will be second
key = Object.keys(names)[index];
value = names[key];
console.log(key + ' -> ' + value);威尔输出
Ben -> 37https://stackoverflow.com/questions/34815310
复制相似问题