0
//Get message from textarea var msg = $('#mytextarea').val(); //Convert string to array of letters // eg. cata = ['c','a','t','a'] var msgLettersAsArray = msg.split(''); 

What I need to do now is replace the single letters,something like this

c = b; a = e; t = c; a = e; //array neeeds to be converted from this: var array = ['c','a','t','a']; // to this: var array = ['b','e','c','e']; 

Is there any way to achieve this? All I need to do is replace the letters that are already in the array with letters of my choice

3
  • 2
    Could you just replace the letters bevor splitting the String? msg.replace(/c/g, 'b') Commented Apr 5, 2014 at 14:18
  • yes, but in the "real" code I need to replace all letters from a-z,0-9 using the replace method would require me to call the replace function for every single letter I need to change.Am I wrong? Commented Apr 5, 2014 at 14:22
  • it is possible with one call to replace(): var map = {c: 'b', a: 'e', t: 'c'}; msg.replace(/[a-z0-9]/g, function (i) { return map[i] || i; }) Commented Apr 5, 2014 at 14:29

7 Answers 7

3

It's quite simple, just define a translation map and use Array.prototype.map.

var translationMap = { c: 'b', a: 'e', t: 'c' }; //returns ['b','e','c','e'] ['c','a','t','a'].map(function (letter) { return translationMap[letter] || letter; }); 

EDIT: It seems you actually just wanted to replace letters in the string, in this case @phylax answer would be correct. There is no need to use arrays for a simple string replacement.

Sign up to request clarification or add additional context in comments.

2 Comments

Please consider the case that letter is not in the translationMap before I give you an upvote :-)
@Bergi OP has fixed replacemtn map
1
function replaceChars(str, map) { var i, reg = ""; for (i in map) reg += i; return str.replace( new RegExp("["+reg.replace(/(\]|-|\\)/,"\\$1")+"]",'g'), function(char) { return map[char]; } ); } 
//Get message from textarea var msg = $('#mytextarea').val(); // "cata" replaceChars(msg, {c:'b', a:'e', t:'c', a:'e'}); // "bece" 

2 Comments

Did he want a string or an array? I am confused. Anyway at worst it's just a question of playing with split and join ;)
@plalx: The .map() solution was too trivial and there was a String.prototype.replaceChars function in my personal helper lib so I just posted it :-)
0

Just making an answer out of my comment:

Like OP said, its ok to be done without the split(). And its possible to do with only one call to String.replace():

var map = { c: 'b', a: 'e', t: 'c' }; msg.replace(/[a-z0-9]/g, function (i) { return map[i] || i; }) 

The RegExp can possibly made event simpler:

msg.replace(/./g, function (i) { return map[i] || i; }) 

2 Comments

That's what happens when the OP doesn't explain what he needs ;)
you are completely right, you deserve the best answer anyway
0

Sure, just use a for loop:

var array = ['c','a','t','a']; for (var i = 0; i < array.length; i++) { var cur = array[i]; if (cur == 'c') { array[i] = 'b'; } else if (cur == 'a') { array[i] = 't'; } else if (cur == 't') { array[i] = 'c'; } } 

But using an object to store these mappings can make your code even more compact:

var array = ['c','a','t','a']; var transform = { 'c': 'b', 'a': 'e', 't': 'c' }; for (var i = 0; i < array.length; i++) { array[i] = transform[array[i]]; } 

Comments

0

not tested but it should work

 var replaxe = { 'c':'b', 'e':'d' }, array = ['c','e'], result = []; for(var item in array){ result.push(replaxe[item]); } console.log(result); 

Comments

0
RUN THIS IN YOUR FIRE BUG CONSOLE var array = ['c','a','t','a']; var myarray = []; for(i=0; i<=array.length; i++) { if(array[i] == 'c' ) { array[i] = 'b' } if(array[i] == 'a' ) { array[i] = 'e' } if(array[i] == 't' ) { array[i] = 'c' } if(array[i] == 'a' ) { array[i] = 'a' } } 

console.log(myarray);

1 Comment

Please use a reasonable indentation, put your text out of the code, and don't shout (caps).
0

I would recommend using a switch-case for every element in the array.

for (i in array) { switch (array[i]) { case "c": array[i] = "b"; break; case "a": array[i] = "e"; break; case "t": array[i] = "c"; break; } } 

2 Comments

replace? Seriously? Also don't use for in on arrays
@Bergi That was really stupid of me. Just a bit tired after a busy day.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.