I want to populate an object with the run length encoding of a string. Using an array this would generally be pretty simple, but I'm not quite sure what to do for an object.
function addto (string) {
var string = string.split("").sort();
var cnt = 1;
var obj = {};
for (i = 0; i < string.length; i ++) {
if (string[i] === string[i+1])
cnt++;
}
return obj;
}
addto("abbccc");
Now obviously I can't use a method like push() here. But I also can't set the object name to the string at position i like so:
obj.string[i] = cnt;
Which is what I initially hoped to do. Is there a proper way to do this so that my output would be (for the example given):
{"a":1, "b":2, "c":3}