gpt4 book ai didi

javascript 获取数组名称

转载 作者:行者123 更新时间:2023-12-03 18:35:12 29 4
gpt4 key购买 nike

所以我有这个数组:

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

我想像这样运行一个 for 循环:

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

这可能吗?如何?你会以不同的方式做到这一点吗?怎么办?

非常感谢

最佳答案

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

它很慢。如果您的浏览器编译正则表达式(就像大多数新的一样),最快的解决方案可能是每个字符使用一个正则表达式:

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

关于javascript 获取数组名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5128119/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com