Hash keys / values โ€‹โ€‹as an array

I can't find javascript equivalent for php array_keys() / array_values()

For people unfamiliar with php, the following js hash is specified:

 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 values, i.e.

 [3, 4, 42] 

You can use jQuery.

+48
javascript hashmap
May 02 '12 at 13:50
source share
9 answers
 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); 
+41
May 02 '12 at 13:53
source share

In ES5 supported (or customized) browsers ...

 var keys = Object.keys(myHash); var values = keys.map(function(v) { return myHash[v]; }); 



Gaskets from MDN ...

+56
May 02 '12 at 13:53
source share

The second answer (at the time of writing) gives:

 var values = keys.map(function(v) { return myHash[v]; }); 

But I prefer to use jQuery's own $.map :

 var values = $.map(myHash, function(v) { return v; }); 

Since jQuery does cross-browser compatibility. Plus itโ€™s shorter :)

In any case, I always try to be as functional as possible. Single line is nicer than loops.

+12
Jan 17 '14 at 2:39
source share

look at the _.keys () and _.values โ€‹โ€‹() functions in lodash or underscore

+8
Feb 11 '14 at 14:29
source share
 function getKeys(obj){ var keys = []; for (key in obj) { if (obj.hasOwnProperty(key)) { keys[keys.length] = key; } } return keys; } 
+3
May 2 '12 at 1:54 pm
source share

I donโ€™t know if this helps, but โ€œforeachโ€ goes through all the keys: for (var key in obj1) {...}

+1
May 02 '12 at 13:54
source share

Here are the implementations from phpjs.org :

This is not my code, I am just pointing you to a useful resource.

+1
May 02 '12 at 13:55
source share

Here is a good example of array_keys from the 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 applies to 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; } 

EDIT: Removed unnecessary sentences from code.

0
May 02 '12 at 13:55
source share
 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) 

mostly

 array=(function(e){a=[];for (var i in e) a.push(e[i]); return a;})(HASHHERE) 
0
09 Oct '13 at 15:56
source share



All Articles