我有一个字符串数组:
["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"]
我想看看上面数组中的任何字符串是否有来自另一个数组的子字符串:
["South Africa", "United States"]
如果是,请执行xyz。
我尝试过使用_.difference(lodash)和_.intersection(下划线),但我不认为它会比较子串的字符串。
P.S。对于疯狂的数组字符串感到抱歉 - 它们是来自返回的JSON的新闻文章。
感谢您的帮助......
答案 0 :(得分:1)
if(_.intersection(arr, check).length > 0){
//do something
}else {
//do something
}
你可以使用javascript的indexOf方法
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);
答案 1 :(得分:1)
这是一个执行此操作的单行程序(使用ES6箭头功能,因此如果您没有进行转换,则可能需要将其重写为常规function(){}
)。你根本不需要lodash
。
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)));
或使用lodash
(_.includes
和_.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)));
答案 2 :(得分:1)
lodash#intersectionWith
:此方法与
_.intersection
类似,只是它接受比较器 调用它来比较数组的元素。订单和 结果值的引用由第一个数组确定。该 比较器使用两个参数调用:(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;
});
});
答案 3 :(得分:0)
Array.indexOf()
它会帮助你。迭代新闻数组并通过迭代国家/地区数组找到国家/地区名称索引。