Reputation: 900
I am getting keys and values from php associative array to jquery object:
$names = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
$js = json_encode($names);
echo 'var names= ' . $js . ";";
Now I know how to iterate through all of the names and get their key and value:
$.each(names, function(key, value){
alert(key + value);
});
But I need only specific value. For example how can I get only "Ben" and "37" without iterating through all the names?
Upvotes: 0
Views: 4194
Reputation: 7283
You can do this using 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);
Will output
Ben -> 37
Upvotes: 4