I am looking to create a JavaScript object (associative array) with alphabets as keys. What is the best way to accomplish this?
Example -
obj[a] = 'somevalue'
obj[b] = 'somevalue'
...
obj[z]= 'some value'
Assigning alphabets as keys dynamically.
I am looking to create a JavaScript object (associative array) with alphabets as keys. What is the best way to accomplish this?
Example -
obj[a] = 'somevalue'
obj[b] = 'somevalue'
...
obj[z]= 'some value'
Assigning alphabets as keys dynamically.
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);
fromCharCode which does not exist on PHP. So you can use the custom function answered at stackoverflow.com/a/9878531/684932 to do so and works like a charm.var hash = {};
hash["abcdefghijklmnopqrstuvwxyz"] = "something";
hash["בגדהוזחטיךכלםמןנסעףפץצקרשת"] = "something else";
hash["АБВГДЕЖЅZЗИІКЛМНОПРСТȢѸФХѾЦЧШЩЪꙐЬѢꙖѤЮѦѪѨѬѠѺѮѰѲѴ"] = "something else";
var obj = { };
obj['A'] = 'letter A';
obj['B'] = 'letter B';
obj['C'] = 'letter C';
or:
var obj = [ ];
obj['A'] = 'letter A';
obj['B'] = 'letter B';
obj['C'] = 'letter C';
and then:
alert(obj.B);
or the equivalent:
alert(obj['B']);
I would use the { } syntax for non-integer based indexes though. Why? Because there are some real gotchas when you use [ ] with non-integer indexes, like this one:
var obj = [ ];
obj['A'] = 'letter A';
obj['B'] = 'letter B';
obj['C'] = 'letter C';
alert(obj.length);
Guess what will be printed? Yes, you guessed it: 0.
[]) or an Object ({}) knowing that an Array is actually an Object with some gotchas. So be careful. Use arrays for integer based indexes and objects for non-integer base indexes.[] for arrays and {} for objects, not to call new Object() and new Array(). The constructors behave in... unexpected ways with certain combinations of parameters. (Documented and reliable unexpected ways, but every time you see that construct, you have to ask "what are they trying to do?")First create an array of letters using the trick Thack Mai used:
var associateArray = []
for (i = 65; i <= 90; i++) {
associateArray[i-65] = String.fromCharCode(i).toLowerCase()
}
Then map values to any of the letters you want to map values to
associateArray['a'] = 1
associateArray['b'] = 2
This creates the type of object used in browsers for CSSStyleDeclaration. It can be iterated over like so
for (var i = 0; i < associateArray.length; i++) {
console.log(associateArray[associateArray[i]])
}
You can use reduce to create an associative array:
const dict = Array.from(new Array(26))
.reduce((p, c, i) => (p[String.fromCharCode(i + 97)] = i, p), {})
console.log(dict)
function range( start, limit ){
var assoc_array = {};
var step = 1;
var DEFAULT_VALUE = "some value";
step *= limit.charCodeAt(0) - start.charCodeAt(0) > 0 ? 1:-1;
while( start !== limit ){
assoc_array[ start ] = DEFAULT_VALUE;
start = String.fromCharCode(start.charCodeAt(0)+step);
}
assoc_array[ limit ] = DEFAULT_VALUE;
return assoc_array;
}
//Usage examples
var alphabet_array = range('a','z');
var reverse_alphabet_array = range('z','a');
console.group("Alphabets form (A-Z) are:");
var a = 65;
for (var i = 0; i<26; i++) {
console.log(String.fromCharCode(a + i))
}
console.groupEnd();
console.group("Alphabets form (a-z) are:");
var a = 97;
for (var i = 0; i<26; i++) {
console.log(String.fromCharCode(a + i))
}
console.groupEnd();