2

so i have this array:

var utf = Array(
 a: Array('á','à','ã','Á','À','Ã'),
 e: Array('é','ê','É','È'),
 i: Array('í','Í'),
 o: Array('ó','õ','Ó','Õ'),
 u: Array('ú','Ú'),
 c: Array('ç','Ç') 
);

I want to run a for loop like:

for(i = 0; i < utf.length; i++){
 for (j = 0; j < utf[i].length; j++){
 mystring.replace(utf[i][j], <utf[i][arrayname]>);
 }
}

is this possible? how? would you do this in a different way? how?

Thank you very much

Lightness Races in Orbit
387k77 gold badges672 silver badges1.1k bronze badges
asked Feb 26, 2011 at 16:27

4 Answers 4

5
function doReplace(mystring)
{
 var utf = {
 a: ['á','à','ã','Á','À','Ã'],
 e: ['é','ê','É','È'],
 i: ['í','Í'],
 o: ['ó','õ','Ó','Õ'],
 u: ['ú','Ú'],
 c: ['ç','Ç']
 };
 for(var c in utf)
 {
 var charArray = utf[c];
 for (var j = 0; j < charArray.length; j++)
 {
 mystring= mystring.replace(new RegExp(charArray[j], "g"), c);
 }
 }
 return mystring;
}

It's slow. The fastest solution if your browser compile regexes (like most new ones do) might be to use one regex per character :

var utf = {
 a: ['á','à','ã','Á','À','Ã'],
 e: ['é','ê','É','È'],
 i: ['í','Í'],
 o: ['ó','õ','Ó','Õ'],
 u: ['ú','Ú'],
 c: ['ç','Ç']
};
var utfRegexes = {};
// Sadly javascript isn't C# so something that could be done in two lines
// of LINQ need to be unrolled.
for(var c in utf)
{
 console.log("in " + c);
 var chars = "[";
 for (var j = 0; j < utf[c].length; j++)
 {
 chars += utf[c][j];
 }
 chars += "]";
 utfRegexes[c] = new RegExp(chars, "g");
}
function doReplaceRegex(mystring)
{ 
 for(var c in utfRegexes)
 {
 mystring = mystring.replace(utfRegexes[c], c); 
 }
 return mystring;
}
answered Feb 26, 2011 at 16:39
Sign up to request clarification or add additional context in comments.

2 Comments

Note that replace will only replace the first instance of the given char/string.
Oh yeah could also solve this bug as I already solved the fact that replace return it's result instead of changing the input as OP seemed to assume.
2

Arrays in Javascript support only integer indexes. Use object. I also recommend using JSON as the most readable and simplest way how to create new arrays and objects:

var utf = {
 a: ['á','à','ã','Á','À','Ã'],
 e: ['é','ê','É','È']
 //etc
};
for (var i in utf)
{
 //In the i variable, you'll find name of array of chars: a, e, i, o, u, c...
 //The array of chars can be found in utf[i]
}
answered Feb 26, 2011 at 16:38

Comments

0

It is possible using an associative array (i.e. object) instead of a normal array.

var utf = {
 'a': ['á','à','ã','Á','À','Ã'],
 'e': ['é','ê','É','È'],
 'i': ['í','Í'],
 'o': ['ó','õ','Ó','Õ'],
 'u': ['ú','Ú'],
 'c': ['ç','Ç']
 }
 var mystring = 'abgvÁÓÚ';
 for(var i in utf){
 for(var j = 0; j < utf[i].length; j++){
 mystring.replace(utf[i][j], i);
 }
 }
answered Feb 26, 2011 at 16:43

Comments

0
// Just a semantic reformat, lacks speed improvements
var utf = Array(
 Array('a', 'á','à','ã','Á','À','Ã'),
 Array('e', 'é','ê','É','È'),
 Array('i', 'í','Í'),
 Array('o', 'ó','õ','Ó','Õ'),
 Array('u', 'ú','Ú'),
 Array('c', 'ç','Ç') 
);
for(i = 0; i < utf.length; i++){
 for j = 1; j < utf[i].length; j++){
 mystring.replace(utf[i][j], utf[i][0]);
 }
}
answered Feb 26, 2011 at 16:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.