thinking_hydrogen thinking_hydrogen - 2 months ago 8
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

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);

console.log(charArray);

http://jsfiddle.net/V2B7S/