Mor
Mor

Reputation: 229

javascript change chars in a string

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

Answers (4)

raphaëλ
raphaëλ

Reputation: 6523

temp.split("").map ( function (e) { return e.charCodeAt(0)-97 }).join("")

Upvotes: 1

Barmar
Barmar

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

Prabhu Murthy
Prabhu Murthy

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

mildog8
mildog8

Reputation: 2154

var temp = "abcdefg"
var tempArray = temp.split("")

tempArray[2] // -> "c"

Upvotes: 0

Related Questions