Reputation: 229
I'm trying to convert a char string to an array like this : "
var temp = "abcdefg
and I want to get temp = 0123456
when:
a=0
b=1
c=2
d=3
e=4
f=5
g=6
any idea? Thanks,
Upvotes: 0
Views: 57
Reputation: 6523
temp.split("").map ( function (e) { return e.charCodeAt(0)-97 }).join("")
Upvotes: 1
Reputation: 781210
var letters = {
a: '0',
b: '1',
c: '2',
d: '3',
e: '4',
f: '5',
g: '6'
};
var input = "abcdefg";
var result= input.split('').map(function(letter) {
return letters[letter];
}).join('');
Upvotes: 1
Reputation: 9261
var temp = "abcdefg";
var arr = temp.split("");
var result =[];
arr.forEach(function(item,index,arr){
result.push(index);
});
result --> [0, 1, 2, 3, 4, 5, 6]
result.join('') --> "0123456"
Upvotes: 0
Reputation: 2154
var temp = "abcdefg"
var tempArray = temp.split("")
tempArray[2] // -> "c"
Upvotes: 0