如何避免循环遍历数组来查找部分匹配?

发布于 2024-10-18 13:24:51 字数 1379 浏览 2 评论 0原文

我正在循环遍历英语短语数组,如果我找到与当前文本节点的匹配项,我会将其替换为 non_english 数组中的翻译。所有这些都可以 100% 准确匹配。

但对于部分匹配,我需要使用 .match 命令,它允许部分匹配。

我搜索完全匹配的代码如下:

var 找到 = $.inArray(value,en_lang);

然后,如果找到值,则替换文本。这种方法很快,我喜欢它。

但是,要进行部分单词/短语匹配,我必须使用此循环代码。

// loop thru language arrays
for (var x = en_count; x > 0; x--) {

// assign current from/to variables for replace
var from = en_lang[x];
var to = other_lang[x];

// if value match do translation
if (value.match(from)) {
    content(node, value.replace(from, to));
}

// mark this node as translated
if ($.browser.msie == 'false') {
    $(node).data('translated', 'yes');
}

可以完成工作,但速度相当慢。经过大量研究,我发现我可以通过 join 命令将英语数组转换为基于列表的字符串。

但我无法想出一个函数来搜索此列表中的部分匹配项,并返回列表中的位置。

我正在尝试这个 2006 年创建的旧 js 函数。但我不知道如何正确地恢复位置。

function listfind(list, value, delimiters) {

    if (!delimiters) {
        var delimiters = ','
    }

    _TempListSplitArray = list.split(delimiters)

    var FoundIdx = 0;

    for (i = 0; i < _TempListSplitArray.length; i++) {
        if (_TempListSplitArray[i] == value) {
            FoundIdx = i + 1;
            break
        }
        if (value.match(_TempListSplitArray[i])) {
            FoundIdx = i + 1;
            break
        }
    }
    return FoundIdx
}

感谢您抽出时间。

I am looping through an array of english phrases, and if i find a match, with the current text node, i replace it with it's translation in the non_english array. All of that works 100% for exact matches.

But for partial matches, I need to use the .match command, which allows for partial matches.

My code to search for exact matches is like this:

var found = $.inArray(value,en_lang);

Then if there is a found value, then do replacement of text. This method is fast and I love it.

However to do partial word/phrase matching, I have to use this looping code.

// loop thru language arrays
for (var x = en_count; x > 0; x--) {

// assign current from/to variables for replace
var from = en_lang[x];
var to = other_lang[x];

// if value match do translation
if (value.match(from)) {
    content(node, value.replace(from, to));
}

// mark this node as translated
if ($.browser.msie == 'false') {
    $(node).data('translated', 'yes');
}

}

This does the job but is pretty slow. After a lot of research, I have found that I can convert the english array to a list-based string via the join command.

But I am unable to come up with a function to search this list for a partial match, and return the position in the list.

I was trying out this old js function created in 2006. But I can't figure out how to get the position back, correctly.

function listfind(list, value, delimiters) {

    if (!delimiters) {
        var delimiters = ','
    }

    _TempListSplitArray = list.split(delimiters)

    var FoundIdx = 0;

    for (i = 0; i < _TempListSplitArray.length; i++) {
        if (_TempListSplitArray[i] == value) {
            FoundIdx = i + 1;
            break
        }
        if (value.match(_TempListSplitArray[i])) {
            FoundIdx = i + 1;
            break
        }
    }
    return FoundIdx
}

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2024-10-25 13:24:51

Javascript 有一个 foreach 类型的系统,但它仍然基于循环

var array = ['hello', 'world'];
for(var key in array){
  alert(array[key]);
}

这是循环数组的最佳选择,但这种方式也适用于对象

var obj = {'one':'hello', 'two':'world'];
for(var key in obj){
   alert("key: "+key+" value: "+obj[key]);
}

更新了对您问题的评论
您只需替换您知道的文字即可

var str = "hello World";
str = str.replace("hello", "Bye bye");
alert(str);

Javascript has a foreach type of system but its still based on a loop

var array = ['hello', 'world'];
for(var key in array){
  alert(array[key]);
}

Thats the best your getting for looping though an array but this way allso works with objects

var obj = {'one':'hello', 'two':'world'];
for(var key in obj){
   alert("key: "+key+" value: "+obj[key]);
}

UPDATED for Comments on your question
You can just replace the text you know

var str = "hello World";
str = str.replace("hello", "Bye bye");
alert(str);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文