thinking_hydrogen thinking_hydrogen - 1 year ago 87
Javascript Question

Javascript - Associative array with alphabets as keys

I am looking to create a Javascript object (associative array) with alphabets as keys. Your inputs for the best way to accomplish this will be much appreciated.

Example -

obj[a] = 'somevalue'
obj[b] = 'somevalue'
obj[z]= 'some value'

Assigning alphabets as keys dynamically.

Thanks in advance.

Answer Source

Here's a quick and lazy way to create the object:

var a = 97;
var charArray = {};
for (var i = 0; i<26; i++)
    charArray[String.fromCharCode(a + i)] = String.fromCharCode(a + i);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download