Есть два массива
["Hello. This is line 1 of text.", "and this is another.", "the end", "line 3 here"]
["the end", "matches", "line 3", "and this is anoother."]
Нужно вывести,если есть совпадение где то в строке.
результат должен быть:
['line 3 here','the end','and this is another.']
Используйте комбинацию из indexOf и filter:
var arr1 = ["Hello. This is line 1 of text.", "and this is another.", "the end", "line 3 here"],
arr2 = ["the end", "matches", "line 3 here", "and this is another."],
intersect = function(arr1, arr2) {
return arr1.filter(function(n) {
return arr2.indexOf(n) !== -1;
});
};
console.log(intersect(arr1, arr2));
Решение на чистом javascript
var first = ["Hello. This is line 1 of text.", "and this is another.", "the end", "line 3 here"];
var second = ["the end", "matches", "line 3", "and this is anoother."];
for (var i = 0; i < first.length; i++) { //проходимся по первому масиву
for (var j = 0; j< second.length; j++) { // ищем соотвествия во втором массиве
if(first[i] === second[j]){
console.log(first[i]); // если совпадаем делаем что либо с этим значением
}
}
}
Продвижение своими сайтами как стратегия роста и независимости