Reputation: 357
I have an array of strings:
["General election 2017: No cut to UK aid spending, says May", "Paris Champs Elysees attack gunman named as Karim Cheurfi", "William and Kate surprise Radio 1 visit", "South Africa bus crash 'kills at least 19 children'", "Len McCluskey re-elected as Unite general secretary", "Cleethorpes car crash captured on CCTV", "Adam Johnson prison video investigated", "Julian Assange's arrest a 'priority' for US attorney general", "Shrewsbury trust warned over baby heart monitoring in 2007", "High heels row: Petition for work dress code law rejected"]
I want to see if any string in the above array has a substring from another array:
["South Africa", "United States"]
If it does, do xyz.
I've tried using _.difference (lodash) and _.intersection (underscore), but I don't think it compares the strings for substrings.
P.S. Sorry for the crazy array strings - they are news articles from a returned JSON.
Thanks for your help...
Upvotes: 0
Views: 743
Reputation: 2955
lodash#intersectionWith
:This method is like
_.intersection
except that it accepts comparator which is invoked to compare elements of arrays. The order and references of result values are determined by the first array. The comparator is invoked with two arguments:(arrVal, othVal)
.
_.intersectionWith(news, words, (article, word) => article.includes(word));
news.filter(article => words.some(word => article.includes(word)));
news.filter(function(article){
return words.some(function(word){
return article.indexOf(word) > -1;
});
});
Upvotes: 1
Reputation: 18389
Here is a one-liner to do this (with ES6 arrow functions, so you might need to rewrite it to regular function(){}
if you are not transpiling). You dont need lodash
at all.
var a = ["General election 2017: No cut to UK aid spending, says May", "Paris Champs Elysees attack gunman named as Karim Cheurfi", "William and Kate surprise Radio 1 visit", "South Africa bus crash 'kills at least 19 children'", "Len McCluskey re-elected as Unite general secretary", "Cleethorpes car crash captured on CCTV", "Adam Johnson prison video investigated", "Julian Assange's arrest a 'priority' for US attorney general", "Shrewsbury trust warned over baby heart monitoring in 2007", "High heels row: Petition for work dress code law rejected"];
var b = ["South Africa", "United States"];
var match = a.filter(source => b.some(substring => source.includes(substring)));
Or with lodash
(_.includes
and _.filter
):
var a = ["General election 2017: No cut to UK aid spending, says May", "Paris Champs Elysees attack gunman named as Karim Cheurfi", "William and Kate surprise Radio 1 visit", "South Africa bus crash 'kills at least 19 children'", "Len McCluskey re-elected as Unite general secretary", "Cleethorpes car crash captured on CCTV", "Adam Johnson prison video investigated", "Julian Assange's arrest a 'priority' for US attorney general", "Shrewsbury trust warned over baby heart monitoring in 2007", "High heels row: Petition for work dress code law rejected"];
var b = ["South Africa", "United States"];
var match = _.filter(a, source => b.some(substring => _.includes(source, substring)));
Upvotes: 1
Reputation: 914
if(_.intersection(arr, check).length > 0){
//do something
}else {
//do something
}
you can use indexOf method from javascript
var arr = ["General election 2017: No cut to UK aid spending, says May", "Paris Champs Elysees attack gunman named as Karim Cheurfi", "William and Kate surprise Radio 1 visit", "South Africa bus crash 'kills at least 19 children'", "Len McCluskey re-elected as Unite general secretary", "Cleethorpes car crash captured on CCTV", "Adam Johnson prison video investigated", "Julian Assange's arrest a 'priority' for US attorney general", "Shrewsbury trust warned over baby heart monitoring in 2007", "High heels row: Petition for work dress code law rejected"];
var check = ["southa frice", "usa"];
var found = false;
for (var i = 0; i < check.length; i++) {
if (arr.indexOf(check[i]) > -1) {
found = true;
break;
}
}
console.log(found);
Upvotes: 1
Reputation: 753
Array.indexOf()
It will helps you. Iterate through news array and find the country name index is present by iterating country array.
Upvotes: 0