I want to split a string by no delimiter (effectively splitting it by each character), then index into an object in alphabetical order. I want it to utilize jQuery.
Here is my attempt, but this splits into an array, and elements are accessed by square brackets (obj_to_chars[0], obj_to_chars[1], etc), but I want to be able to access elements by using the dot notation, like so: obj_of_chars.a, obj_of_chars.b, etc.
var str = 'The quick brown fox jumps over the lazy dog.';
var obj_of_chars = {};
obj_of_chars = str.split("");
console.log(obj_of_chars);
// what I have: [ "T", "h", "e", " ", "q", "u", "i", "c", "k", " ", "b", "r", "o", "w", "n", " fox", " ", "j", "u", "m", "p", "s", " ", "o", "v", "e", "r", " ", "t", "h", "e", " ", "l", "a", "z", "y", " ", "d", "o", "g", "." ]
// what I want: { a: "T", b: "h", c: "e", d: " ", e: "q", ..., aa, ab, ... }
EDIT: The solution should allow more than 26 characters in the object. (sorry to those who already wrote a piece of code, just realized I needed more than 26 characters)
Here’s one way to do it:
If the length of the input string is more than 26, this code will spill over the alphabet and start using non-alpha characters as keys. Modify the expression that sets
propaccordingly if you want to avoid this.Update: Here’s how to continue the alphanumeric property name scheme indefinitely: