简体   繁体   中英

javascript get array name

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

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;
}

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]
}

It is possible using an associative array (ie 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);
        }
    }
// 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]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM