Hash keys / values as array
JavascriptHashmapJavascript Problem Overview
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.
Javascript Solutions
Solution 1 - Javascript
In ES5 supported (or shimmed) browsers...
var keys = Object.keys(myHash);
var values = keys.map(function(v) { return myHash[v]; });
Shims from MDN...
Solution 2 - Javascript
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);
Solution 3 - Javascript
The second answer (at the time of writing) gives:
var values = keys.map(function(v) { return myHash[v]; });
But I prefer using jQuery's own $.map
:
var values = $.map(myHash, function(v) { return v; });
Since jQuery takes care of cross-browser compatibility. Plus it's shorter :)
At any rate, I always try to be as functional as possible. One-liners are nicers than loops.
Solution 4 - Javascript
Look at the _.keys() and _.values() functions in either Lodash or Underscore.js:
Solution 5 - Javascript
function getKeys(obj){
var keys = [];
for (key in obj) {
if (obj.hasOwnProperty(key)) { keys[keys.length] = key; }
}
return keys;
}
Solution 6 - Javascript
I don't know if it helps, but the "foreach" goes through all the keys:
for (var key in obj1) {...}
Solution 7 - Javascript
Here are implementations from phpjs.org:
This is not my code. I'm just pointing you to a useful resource.
Solution 8 - Javascript
Use:
var myHash = {"apples": 3, "oranges": 4, "bananas": 42}
vals=(function(e){a=[];for (var i in e) a.push(e[i]); return a;})(myHash).join(',')
keys=(function(e){a=[];for (var i in e) a.push( i ); return a;})(myHash).join(',')
console.log(vals,keys)
Basically:
array=(function(e){a=[];for (var i in e) a.push(e[i]); return a;})(HASHHERE)
Solution 9 - Javascript
Here is a good example of array_keys
from PHP.js library:
function array_keys (input, search_value, argStrict) {
// Return just the keys from the input array, optionally only for the specified search_value
var search = typeof search_value !== 'undefined',
tmp_arr = [],
strict = !!argStrict,
include = true,
key = '';
for (key in input) {
if (input.hasOwnProperty(key)) {
include = true;
if (search) {
if (strict && input[key] !== search_value) {
include = false;
}
else if (input[key] != search_value) {
include = false;
}
}
if (include) {
tmp_arr[tmp_arr.length] = key;
}
}
}
return tmp_arr;
}
The same goes for array_values
(from the same PHP.js library):
function array_values (input) {
// Return just the values from the input array
var tmp_arr = [],
key = '';
for (key in input) {
tmp_arr[tmp_arr.length] = input[key];
}
return tmp_arr;
}