Getting a list of associative array keys

I have an associative array in Javascript:

var dictionary = {
    "cats": [1,2,3,4,5], 
    "dogs": [6,7,8,9,10]
};

How do I get this dictionary's keys? i.e. I want

var keys = ["cats", "dogs"];

Edit 7 years later: Just to get the terminology correct - there is no such thing as an 'associative array' in Javascript - this is technically just an object and it is the object keys we want.

Answers:

Answer

You can use: Object.keys(obj)

Example:

var dictionary = {
  "cats": [1, 2, 37, 38, 40, 32, 33, 35, 39, 36],
  "dogs": [4, 5, 6, 3, 2]
};

// Get the keys
var keys = Object.keys(dictionary);

console.log(keys);

See reference below for browser support. It is supported in Firefox 4.20, Chrome 5, IE9. The link below contains a code snippet that you can add if Object.keys() is not supported in your browser.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys

Answer
for (var key in dictionary) {
  // do something with key
}

It's the for..in statement.

Answer

Just a quick note, be wary of using for..in if you use a library (jQuery, prototype, etc.), as most of them add methods to created Objects (including dictionaries).

This will mean that when you loop over them, method names will appear as keys. If you are using a library, look at the documentation and look for an enumerable section, where you will find the right methods for iteration of your objects.

Answer

Simple JQUERY way.

This is what I use
DictionaryObj being the javascript dictionary object you want to go through. value, key ofcourse being the names of them in the dictionary.

 $.each(DictionaryObj, function (key, value) {
            $("#storeDuplicationList")
                .append($("<li></li>")
                .attr("value", key)
                .text(value));
        });
Answer

I am currently using Rob de la Cruz's reply

Object.keys(obj)

and in a file loaded early on I have some lines of code borrowed from elsewhere on the internet which cover the case of old versions of script interpreters that do not have Object.keys built in.

if (!Object.keys) {
    Object.keys = function(object) {
        var keys = [];
        for (var o in object) {
            if (object.hasOwnProperty(o)) {
                keys.push(o);
            }
        }
        return keys;
    };
}

I think this is the best of both worlds for large projects: simple modern code and backwards compatible support for old versions of browsers etc.

Effectively it puts JW's solution into the function when Rob de la Cruz's Object.keys(obj) is not natively available.

Answer

Try this:

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

hasOwnProperty is needed because it's possible to insert keys into the prototype object of dictionary. But you typically don't want those keys included in your list.

For example, if you do this:

Object.prototype.c = 3;
var dictionary = {a: 1, b: 2};

and then do a for...in loop over dictionary, you'll get a and b, but you'll also get c.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.