Reputation: 4470
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 ?
Upvotes: 1
Views: 519
Reputation: 565
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);
Upvotes: 1
Reputation: 1
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; });
Upvotes: 0
Reputation: 386604
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; }
Upvotes: 1
Reputation: 322
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())));
Upvotes: 0
Reputation: 2645
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;
}
}
Upvotes: 1
Reputation: 5330
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]);
}
}
}
Upvotes: 2