I cannot find the JavaScript equivalent of PHP array_keys() / array_values().
For people unfamiliar with PHP given the following JavaScript hash:
var myHash = {"apples": 3, "oranges": 4, "bananas": 42}  How can I get an array of keys, i.e.,
["apples", "oranges", "bananas"]  The same question with the values, i.e.,
[3, 4, 42]  jQuery can be used.
it can have duplicate values but not keys.
Arrays contains unique key. Hence if u are having multiple value for a single key, use a nested / multi-dimensional array. =) thats the best you got.
Each key can only have one value. But the same value can occur more than once inside a Hash, while each key can occur only once.
In ES5 supported (or shimmed) browsers...
var keys = Object.keys(myHash);  var values = keys.map(function(v) { return myHash[v]; });  Shims from MDN...
Object.keys
Array.prototype.map
var a = {"apples": 3, "oranges": 4, "bananas": 42};      var array_keys = new Array(); var array_values = new Array();  for (var key in a) {     array_keys.push(key);     array_values.push(a[key]); }  alert(array_keys); alert(array_values); 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With