简体   繁体   English

如何在两个不同的文本区域中找到相同的单词并计算它们?

[英]How to find the same words in two different textarea and count them?

I have multiple excel columns with hundreds of data. 我有数百个数据的多个Excel列。 I want to find the same words in these columns. 我想在这些列中找到相同的词。 For example, First Column: dog rat catch act some cat fork apple, Second Column: fork dog lamp song apple some hymn candle,etc., and output would be "dog, some, fork, apple". 例如,第一列:狗抓捕一些猫叉苹果,第二列:叉狗灯歌苹果一些赞美蜡烛,等等,输出将是“狗,一些,叉子,苹果”。

I found this code from Intersecting texts to find common words this thread. 我从相交的文本中找到了此代码, 以找到该线程的常用词 It works for strings but, doesn't work for textareas and inputs. 它适用于字符串,但不适用于textareas和输入。 I tried to modify it for my problem but, I failed. 我试图为我的问题修改它,但是失败了。

Thanks in advance. 提前致谢。

 var t1 = document.getElementById('first').value; var t2 = document.getElementById('second').value; function intersect() { var set = {}; [].forEach.call(arguments, function(a,i){ var tokens = a.match(/\\w+/g); if (!i) { tokens.forEach(function(t){ set[t]=1 }); } else { for (var k in set){ if (tokens.indexOf(k)<0) delete set[k]; } } }); return Object.keys(set); } console.log(intersect(t1, t2)); 
 <textarea id="first"></textarea> <textarea id="second"></textarea> 

 function intersect() {
        let arr1 = t1.split("/*you can split by comma or whitespace*/");
        let arr2 = t2.split("");
        let result = [];
        for (let i = 0; i < arr1.length; i +=1) {
            for (let i1 = 0; i1 < arr2.length; i1 +=1 {
                if (arr1[i] === arr2[i1]) {
                    result.push(arr[i]);
                    break;
                }
            }
        }
        return result;
    }

Try this. 尝试这个。

Here is my attempt: 这是我的尝试:

 function update(){ var result = compare( document.getElementById("first").value, document.getElementById("second").value, " " //Space ); console.log(result); } function compare(haystack, needles, separator) { var result = new Array(); var HaystackArray = haystack.split(separator); var NeedlesArray = needles.split(separator); HaystackArray.forEach(function(needle) { if(!NeedlesArray.includes(needle)) return; //Remove that if you want to allow words to be in the result multiple times if(result.includes(needle)) return; result.push(needle); }); return result; } 
 <textarea id="first" onChange='update()'>Lorem ipsum dolor sit amet, consectetur adipiscing elit</textarea> <textarea id="second" onChange='update()'>Lorem ipsum in hac habitasse platea dictumst consectetur dolor</textarea> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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