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 ?

3
  • Possible duplicate of stackoverflow.com/questions/11076067/… Commented Oct 5, 2016 at 7:30
  • So in common hobbies what should be pushed? cricket or playing cricket Commented Oct 5, 2016 at 7:35
  • if i'm searching with cricket it should push playing cricket or if we search with playing cricket it should push cricket. Commented Oct 5, 2016 at 9:52

6 Answers 6

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]);
            }
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

lowercasing the strings might help too.
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;
    }
}

1 Comment

No problem. As stated above, you should convert the strings to lowercase before comparing them.
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; }

Comments

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);

2 Comments

thanks @sheetal , but it looking for same element in array,not pushing similar elements..
I've updated it..now it finds the similar string in the arrays and push the similar element to the third array
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())));

Comments

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; });

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.