1

I want to compare two string array like the following..

var array1 = ["playing cricket", "Reading Books", "Flying"]

var array2 = ["cricket", "Books"]

var array3 = ["common hobbies will insert here"]

so here I get common hobbies from both arrays like fuzzy search.

Please can anyone tell me how the get these common hobbies in both scenarios ?

Vural
  • 8,666
  • 11
  • 40
  • 57
Naren
  • 4,152
  • 3
  • 17
  • 28
  • Possible duplicate of http://stackoverflow.com/questions/11076067/finding-matches-between-multiple-javascript-arrays – Robin-Hoodie Oct 05 '16 at 07:30
  • So in common hobbies what should be pushed? `cricket` or `playing cricket` – Rajesh Oct 05 '16 at 07:35
  • if i'm searching with cricket it should push playing cricket or if we search with playing cricket it should push cricket. – Naren Oct 05 '16 at 09:52

6 Answers6

2

Fiddle

Try something like

for (i = 0; i < array2.length; i++) {
        for (j = 0; j < array1.length; j++) {
            if (array1[j].toLowerCase().indexOf(array2[i].toLowerCase()) != -1) {
                arra3.push(array1[i]);
            }
        }
    }
Arunkumar
  • 5,150
  • 4
  • 28
  • 40
1

you can also create a function for this

function find_similar_elements(a, b)
{
  var result = [];
  while(a.length>0 && b.length>0){
    if((a[0] > b[0]) || (a[0] < b[0])){
      var min,max; 
      if(a[0].length > b[0].length) {min = b[0]; max=a[0]}
      else{ min = a[0]; max=b[0] }

      if(max.indexOf(min) !== -1){
        result.push(min);
      } 
      a.shift();
      b.shift();

    } else{
      result.push(a.shift())
      b.shift();
    }
  }

  return result;
}

var array1 = ["playing cricket", "Reading Books", "Flying"]

var array2 = ["cricket", "Books"]

var result = find_similar_elements(array1, array2);
sheetal
  • 565
  • 6
  • 13
  • thanks @sheetal , but it looking for same element in array,not pushing similar elements.. – Naren Oct 05 '16 at 10:20
  • I've updated it..now it finds the similar string in the arrays and push the similar element to the third array – sheetal Oct 05 '16 at 11:24
1

You can loop through each array and compare the indexOf for each iteration. See the jsfiddle below.

var array1 = ["playing cricket", "Reading Books", "Flying"];

var array2 = ["cricket", "Books"];

var array3 = [];

for (var i = 0; i < array1.length; i++) {
    for (var x = 0; x < array2.length; x++) {
        console.log(array1[i] + ' ' + array2[x] + ': ' + similar(array1[i], array2[x]));
        if (similar(array1[i], array2[x])) {
            array3.push(array1[i] + ' : ' + array2[x]);
        }
    }
}

for(var c = 0; c < array3.length; c++){
document.write(array3[c] + '<br />');
}


function similar(a, b) {
    if (a.indexOf(b) !== -1 || b.indexOf(a) !== -1) {
        return true
    } else {
        return false;
    }
}
Tom
  • 2,543
  • 3
  • 21
  • 42
  • No problem. As stated above, you should convert the strings to lowercase before comparing them. – Tom Oct 05 '16 at 13:40
1

You could use Array#forEach and iterate both arrays for the check. Then collect common words in an object, to prevent more than one item of each.

var array1 = ["playing cricket", "Reading Books", "Flying"],
    array2 = ["cricket", "Books"],
    common = {};

array1.forEach(function (a) {
    var lowerA = a.toLocaleLowerCase();
    array2.forEach(function (b) {
        var lowerB = b.toLocaleLowerCase();
        if (lowerA.indexOf(lowerB) >= 0) {
            common[lowerB] = true;
        }
        if (lowerB.indexOf(lowerA) >= 0) {
            common[lowerA] = true;
        }
    });
});

console.log(Object.keys(common));

Another soulution could be to get the single words of the arrays, apply a bitmask for the array number and get the common out of the object for remembering.

var items = [["playing cricket", "Reading Books", "Flying"], ["cricket", "Books"]],
    count = {},
    common;

items.forEach(function (a, i) {
    a.forEach(function (b) {
        b.toLocaleLowerCase().split(' ').forEach(function (c) {
            this[c] = (this[c] || 0) | 1 << i;
        }, this);
    }, this);
}, count);

common = Object.keys(count).filter(function (k) {
    return count[k] === (1 << items.length) - 1;
});

console.log(common);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can compare two arrays (array1 and array2) using both filter and indexOf and find the common items:

array1.filter(function(n) { return array2.indexOf(n) != -1; });

  • You should really add some explanation as to why this should work - you can also add code as well as the comments in the code itself - in its current form, it does not provide any explanation which can help the rest of the community to understand what you did to solve/answer the question. This is especially important for an older question and the questions that already have answers. – ishmaelMakitla Oct 05 '16 at 08:00
0

You can use Array#filter:

array1.filter(function(commonHobby) {
    for(var i = 0;i < array2.length;++i) {
        if(commonHobby.toLowerCase().indexOf(array2[i].toLowerCase()) !== -1) return true;
    }
});

In ES7 you can shorten it to:

array1.filter(commonHobby => array2.find(hobby => commonHobby.toLowerCase().includes(hobby.toLowerCase())));
Huntro
  • 322
  • 1
  • 3
  • 16