is there any function that can convert for us a string to an object like this format
{"0":"F","1":"a","2":"t","3":"i","4":"m","5":"a","6":" ","7":"m","8":"a","9":"y","10":"c","11":"h","12":"i","13":"n","14":"e","15":" "}
CodePudding user response:
Enumerate() from python:
string = "Hello World"
enum = enumerate(string)
print(dict(enum))
This should get you:
{0: 'H', 1: 'e', 2: 'l', 3: 'l', 4: 'o', 5: ' ', 6: 'W', 7: 'o', 8: 'r', 9: 'l', 10: 'd'}
CodePudding user response:
If you don't have a problem implementing this function yourself, you can use the following approach in Javascript
:
var s = "Hello World";
var dic = {};
for(var i=0; i<s.length; i ) {
dic[i] = s[i];
}
console.log(dic);
Output:
{0: 'H', 1: 'e', 2: 'l', 3: 'l', 4: 'o', 5: ' ', 6: 'W', 7: 'o', 8: 'r', 9: 'l', 10: 'd'}