Javascript 多次使用JS.split并与数组连接。它只运行最后一对

Javascript 多次使用JS.split并与数组连接。它只运行最后一对,javascript,join,replace,split,Javascript,Join,Replace,Split,我试图使用JS解析keyup上的文本输入字段,以显示所有代词都已更改的相同文本。它应该是:我,我,我的和生产: 你,你,你的 s/他,s/他,她的/他的 他们,他们,他们的 它适用于第一组替换件。在第二组和第三组上,它仅替换数组中的最后一个变量集:( 我确信有一种更有效的方法来编写这段代码。我把每一部分都完全重复了,试图解决我的问题,但没有可用的。我担心我犯了一个简单的错误 如果你想试一试的话,给你一把小提琴。 以下是原始JS: function grammar() { text =

我试图使用JS解析keyup上的文本输入字段,以显示所有代词都已更改的相同文本。它应该是:我,我,我的和生产: 你,你,你的 s/他,s/他,她的/他的 他们,他们,他们的

它适用于第一组替换件。在第二组和第三组上,它仅替换数组中的最后一个变量集:(

我确信有一种更有效的方法来编写这段代码。我把每一部分都完全重复了,试图解决我的问题,但没有可用的。我担心我犯了一个简单的错误

如果你想试一试的话,给你一把小提琴。

以下是原始JS:

function grammar() {

    text = ' ' + document.getElementById("first_p").value + ' ';
    var array = {
        " my ": " your ",
        " myself ": " yourself ",
        " mine ": " yours ",
        " I ": " you ",
        " me ": " you ",
        " our ": " your "
    };
    for (var val in array)
        text = text.split(val).join(array[val]);
    document.getElementById('2_pe').innerHTML = text;

    text2 = ' ' + document.getElementById("first_p").value + ' ';
    var array2 = {
        " my ": " her/his ",
        " myself ": " herself/himself ",
        " mine ": " hers/his ",
        " I ": " s/he ",
        " me ": " s/he ",
        " our ": " her/his "
    };
    for (var val2 in array2)
        text_3pe1 = text2.split(val2).join(array2[val2]);
    document.getElementById('3_pe1').innerHTML = text_3pe1;

    text3 = ' ' + document.getElementById("first_p").value + ' ';
    var array3 = {
        " my ": " their ",
        " myself ": " themself ",
        " mine ": " theirs ",
        " I ": " they ",
        " me ": " they ",
        " our ": " their "
    };
    for (var val3 in array3)
        text_3pe2 = text3.split(val3).join(array3[val3]);
    document.getElementById('3_pe2').innerHTML = text_3pe2;

}

非常感谢您的帮助!这件事让我非常恼火。

您在forEach方法中使用了错误的变量

更改以下行:

text_3pe1 = text2.split(val2).join(array2[val2]);
document.getElementById('3_pe1').innerHTML = text_3pe1;

text_3pe2 = text3.split(val3).join(array3[val3]);
document.getElementById('3_pe2').innerHTML = text_3pe2;
为了

它在做替换,只是在错误的地方


这是你在forEach方法中使用了错误的变量

更改以下行:

text_3pe1 = text2.split(val2).join(array2[val2]);
document.getElementById('3_pe1').innerHTML = text_3pe1;

text_3pe2 = text3.split(val3).join(array3[val3]);
document.getElementById('3_pe2').innerHTML = text_3pe2;
为了

它在做替换,只是在错误的地方


这是你在forEach方法中使用了错误的变量

更改以下行:

text_3pe1 = text2.split(val2).join(array2[val2]);
document.getElementById('3_pe1').innerHTML = text_3pe1;

text_3pe2 = text3.split(val3).join(array3[val3]);
document.getElementById('3_pe2').innerHTML = text_3pe2;
为了

它在做替换,只是在错误的地方


这是你在forEach方法中使用了错误的变量

更改以下行:

text_3pe1 = text2.split(val2).join(array2[val2]);
document.getElementById('3_pe1').innerHTML = text_3pe1;

text_3pe2 = text3.split(val3).join(array3[val3]);
document.getElementById('3_pe2').innerHTML = text_3pe2;
为了

它在做替换,只是在错误的地方


这里是小提琴您没有通过for循环每次更新
text2
text3
,因此只有最后的更改会保存到
text\u 3pe1
text\u 3pe2
。将
text\u 3pe1
替换为
text2
text\u 3pe2
替换为
text3
,就像这样,您应该得到您正在查找的结果。

您没有通过for循环每次更新
text2
text3
,因此只有最后的更改会保存到
text\u 3pe1
text\u 3pe2
。将
text\u 3pe1
替换为
text2
,将
text\u 3pe2
替换为
text3
,就像这样,您应该得到您正在查找的结果。

您没有通过for循环每次更新
text2
text3
,因此只有最后的更改保存到
text\u 3pe1
text\u 3pe2
。将
text\u 3pe1
替换为
text2
,将
text\u 3pe2
替换为
text3
,就像这样,您应该这样做获取您要查找的结果。

您没有通过for循环每次更新
text2
text3
,因此只有最后的更改保存到
text\u 3pe1
text\u 3pe2
。将
text\u 3pe1
替换为
text2
,并将
text\u 3pe2
替换为
text3
,就像这样,您应该你可以得到你想要的结果。

正如其他人所说,你没有在for循环中更新正确的变量。但是我认为你应该重新考虑一下你的方法。现在你在循环的每一次迭代中分裂和加入,这有点不必要

这种方法允许您减少类似的代码,并替换临时数组中的单词,然后重新连接:

function grammar() {
    replace(document.getElementById('first_p').value, {
        "my": "your",
            "myself": "yourself",
            "mine": "yours",
            "I": "you",
            "me": "you",
            "our": "your"
    }, document.getElementById('2_pe'));

    replace(document.getElementById('first_p').value, {
        "my": "her/his",
            "myself": "herself/himself",
            "mine": "hers/his",
            "I": "s/he",
            "me": "s/he",
            "our": "her/his"
    }, document.getElementById('3_pe1'));

    replace(document.getElementById('first_p').value, {
        "my": "their",
            "myself": "themself",
            "mine": "theirs",
            "I": "they",
            "me": "they",
            "our": "their"
    }, document.getElementById('3_pe2'));
}

function replace(inputText, replacements, displayElement) {
    var words = inputText.split(' ');

    for (var i = 0; i < words.length; i++) {
        var word = words[i];
        if (replacements[word.trim()]) {
            words[i] = replacements[word].trim();
        }
    }

    displayElement.innerHTML = words.join(' ');
}
函数语法(){ 替换(document.getElementById('first_p')。值{ “我的”:“你的”, “我自己”:“你自己”, “我的”:“你的”, “我”:“你”, “我”:“你”, “我们的”:“你的” },document.getElementById('2_-pe'); 替换(document.getElementById('first_p')。值{ “我的”:“她/他的”, “我自己”:“她/他自己”, “我的”:“她的/他的”, “I”:“s/he”, “我”:“s/he”, “我们的”:“她/他的” },document.getElementById('3_pe1'); 替换(document.getElementById('first_p')。值{ “我的”:“他们的”, “我自己”:“他们自己”, “我的”:“他们的”, “我”:“他们”, “我”:“他们”, “我们的”:“他们的” },document.getElementById('3_pe2'); } 功能替换(输入文本、替换、显示元素){ 变量词=inputText.split(“”); for(var i=0;i

正如其他人所说,您没有在for循环中更新正确的变量。但是,我认为您应该重新考虑一下您的方法。现在您正在拆分和加入循环的每个迭代,这有点不必要

这种方法允许您减少类似的代码,并替换临时数组中的单词,然后重新连接:

function grammar() {
    replace(document.getElementById('first_p').value, {
        "my": "your",
            "myself": "yourself",
            "mine": "yours",
            "I": "you",
            "me": "you",
            "our": "your"
    }, document.getElementById('2_pe'));

    replace(document.getElementById('first_p').value, {
        "my": "her/his",
            "myself": "herself/himself",
            "mine": "hers/his",
            "I": "s/he",
            "me": "s/he",
            "our": "her/his"
    }, document.getElementById('3_pe1'));

    replace(document.getElementById('first_p').value, {
        "my": "their",
            "myself": "themself",
            "mine": "theirs",
            "I": "they",
            "me": "they",
            "our": "their"
    }, document.getElementById('3_pe2'));
}

function replace(inputText, replacements, displayElement) {
    var words = inputText.split(' ');

    for (var i = 0; i < words.length; i++) {
        var word = words[i];
        if (replacements[word.trim()]) {
            words[i] = replacements[word].trim();
        }
    }

    displayElement.innerHTML = words.join(' ');
}
函数语法(){ 替换(document.getElementById('first_p')。值{ “我的”:“你的”, “我自己”:“你自己”, “我的”:“你的”, “我”:“你”, “我”:“你”, “我们的”:“你的” },document.getElementById('2_-pe'); 替换(document.getElementById('first_p')。值{ “我的”:“她/他的”, “我自己”:“她/他自己”, “我的”:“她的/他的”, “I”:“s/he”, “我”:“s/he”, “我们的”:“她/他的” },document.getElementById('3_pe1'); 替换(document.getElementById('first_p')。值{ “我的”:“他们的”, “我自己”:“他们自己”, “我的”:“他们的”, “我”:“他们”, “我”:“他们”, “我们的”:“他们的” },document.getElementById('3_pe2'); } 功能替换(输入文本、替换、显示元素){ 变量词=inputText.split(“”); for(var i=0;i