我不确定标题中的解释是否很好,但这是一个示例:
我有两个数组:我想把arr2中的每个字符串都放在arr1中,看是否有匹配项。
let arr1 = ["Apple","Ape","Orange","Apricot","Pineapple"]
let arr2 = ["Ap","Or"]
我想找回以下数组(以任何顺序):
["Apple","Ape","Orange","Apricot"]
我找到了.includes(),但它仅搜索完全匹配项。
答案 0 :(得分:3)
不需要库,只需filter
遍历单词中是否包含第二个数组的子字符串中的some
,就可以let arr1 = ["Apple","Ape","Orange","Apricot","Pineapple"]
let arr2 = ["Ap","Or"]
console.log(
arr1.filter((word) => arr2.some(substr => word.includes(substr)))
);
:
WITH cte1
AS (SELECT id,sdate,edate from t
union all
select c.id,DATEADD(DAY, 1, c.sdate),c.edate from cte1 c where DATEADD(DAY, 1, c.sdate)<=c.edate
)
SELECT sdate,count(id) as total FROM cte1
group by sdate
OPTION (MAXRECURSION 0)
答案 1 :(得分:1)
传统方法是使用两次迭代来检查indexOf()
let arr1 = ["Apple","Ape","Orange","Apricot","Pineapple"]
let arr2 = ["Ap","Or"]
for(var i in arr1){
for(var j in arr2){
if(arr1[i].indexOf(arr2[j]) == 0){
console.log(arr1[i]);
break;
}
}
}