Javascript:将文本区域的每一行与另一个文本区域的所有行进行比较

发布于 2024-12-26 13:01:41 字数 85 浏览 0 评论 0原文

我需要将文本区域“a”的每一行与文本区域“b”的所有行进行比较,并使用“b”中不存在的“a”的所有项目创建一个alert(),

我该怎么做?

I need to compare each line of textarea "a" with all lines of textarea "b" , and create an alert() with all items of "a" who doesn't exists in "b"

¿ How can i do this ?

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

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

发布评论

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

评论(1

小伙你站住 2025-01-02 13:01:41

一种方法是获取 2 个文本区域的值,并将它们拆分为换行符 (\n)。然后,您可以迭代第一个文本区域的行并检查它们是否出现在第二个文本区域中:

var t1 = document.getElementById("textarea1").value.split("\n"),
    t2 = document.getElementById("textarea2").value.split("\n"),
    notIn2 = [];
for(var i = 0; i < t1.length; i++) {
    if(t2.indexOf(t1[i]) === -1) {
        notIn2.push(t1[i]);  
    } 
}

很可能有更好的方法,而且我确信上面的方法远非完美。这就是首先想到的。这是一个工作示例

One way could be to get the values of the 2 textareas and split them on newline (\n) characters. You can then iterate over the lines of the first textarea and check to see if they appear in the second textarea:

var t1 = document.getElementById("textarea1").value.split("\n"),
    t2 = document.getElementById("textarea2").value.split("\n"),
    notIn2 = [];
for(var i = 0; i < t1.length; i++) {
    if(t2.indexOf(t1[i]) === -1) {
        notIn2.push(t1[i]);  
    } 
}

There may well be a better way, and I'm sure the above is far from perfect. This is just what came to mind first. Here's a working example.

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