How to get a set of keys in a javascript dictionary?

I have a dictionary:

var driversCounter = { "one": 1, "two": 2, "three": 3, "four": 4, "five": 5 } 

Now I need to show it in the drop-down list. How to get a set of keys in my dictionary?

+70
javascript dictionary
May 18 '12 at 14:58
source share
10 answers

Use Object.keys() or the Prized Puck of it in older browsers ...

 const keys = Object.keys(driversCounter); 

If you need values, there is Object.values() and if you need a key and value, you can use Object.entries() , often paired with Array.prototype.forEach() for example like this ...

 Object.entries(driversCounter).forEach(([key, value]) => { console.log(key, value); }); 



Alternatively, given your use case, maybe this will do it ...

 var selectBox, option, prop; selectBox = document.getElementById("drivers"); for (prop in driversCounter) { option = document.createElement("option"); option.textContent = prop; option.value = driversCounter[prop]; selectBox.add(option); } 
+98
May 18 '12 at 15:00
source share

One option uses Object.keys() :

 Object.keys(driversCounter) 

It works great for modern browsers (however, IE has supported it since version 9).

To add compatible support, you can copy the code snippet specified in MDN .

+51
May 18 '12 at 15:00
source share

to skip the "dictionary" (we call it an object in JS), use the for in loop:

 for(var key in driversCounter) { if(driversCounter.hasOwnProperty(key)) { //key = keys, left of the ":" //driversCounter[key] = value, right of the ":" } } 
+19
May 18 '12 at 15:00
source share

This will work in all JavaScript implementations:

 var keys = []; for (var key in driversCounter) { if (driversCounter.hasOwnProperty(key)) { keys.push(key); } } 

As mentioned earlier, you can use Object.keys , but it may not work in older versions. So you can use the following monkey patch:

 if (!Object.keys) { Object.keys = function (object) { var keys = []; for (var key in object) { if (object.hasOwnProperty(key)) { keys.push(key); } } } } 
+9
May 18 '12 at 15:03
source share

use Object.Keys()

 var driversCounter = { "one": 1, "two": 2, "three": 3, "four": 4, "five": 5 } console.log(Object.keys(driversCounter)); 
+4
May 10 '17 at 1:18 p.m.
source share

Using a modern JS engine, you can use Object.keys(driversCounter)

+3
May 18 '12 at 15:00
source share

for new browsers: Object.keys( MY_DICTIONARY ) will return an array of keys. Otherwise, you can go to the old school:

 var keys = [] for(var key in dic) keys.push( key ); 
+3
May 18, '12 at 15:00
source share

As others have said, you can use Object.keys() , but who cares about older browsers, right?

Good, yes.

Try this . array_keys from PHPJS ports PHP is a convenient array_keys function, so it can be used in JS. At first glance, it uses Object.keys if supported, but handles the case when it is not very easy. It even includes filtering keys based on values ​​that you might be looking for (optional), and switching whether to use string comparison === compared to comparing type comparisons == (optional)

+1
May 18 '12 at 15:01
source share

if you can use jquery then

 var keys = []; $.each(driversCounter, function(key, value) { keys.push(key); }); console.log(JSON.stringify(keys)); 

here is the answer:

 ["one","two","three","four","five"] 

and you don’t have to worry whether the browser supports the Object.keys method or not.

+1
Aug 31 '13 at 20:16
source share

Another approach is to use multidimensional arrays:

 var driversCounter = [ ["one", 1], ["two", 2], ["three", 3], ["four", 4], ["five", 5] ] 

and access the value using driverCounter [k] [j], where j = 0.1 in the case.
Add it to the drop-down list:

 var dd = document.getElementById('your_dropdown_element'); for(i=0;i<driversCounter.length-1;i++) { dd.options.add(opt); opt.text = driversCounter[i][0]; opt.value = driversCounter[i][1]; } 
-four
May 18 '12 at 15:10
source share



All Articles