0

I have multiple excel columns with hundreds of data. 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. 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>
HDkilic
  • 17
  • 1
  • 4

2 Answers2

0
 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.

0

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>
ZBAGI
  • 128
  • 6