比较子字符串数组 [英] Compare arrays of sub-string
本文介绍了比较子字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查字符串数组是否包含存储在另一个字符串数组中的单词的一部分?
How can I check if an array of strings contains part of words stored in another array of strings ?
假设我已经声明了这三个数组:
Let's say I've declared these three arrays :
array1 = ["Lorem", "ipsum", "dolor" "sit" "amet"]
array2 = ["Lorem", "ipsum"]
keywords = ["ipsum", "dol"]
在将 array1
与关键字
进行比较时,我想得到类似 true
的东西,因为"ipsum"
和<在将 array2
与 keywords
进行比较时,code>"dol" 位于 array1
中,但 false
"dol"
不在 array2
When comparing array1
with keywords
, I want to get something like true
because "ipsum"
and "dol"
are in array1
, but false
, when comparing array2
with keywords
because "dol"
isn't in array2
我搜索了一个小时,但是我不知道该怎么做...我成功地比较了带有一个关键字而不是多个关键字的数组.
I searched for an hour, but I don't know how to do it... I succeeded to compare arrays with one keyword, but not with several keywords.
推荐答案
- 对于关键字数组中的每个元素(使用
.every()
) - 其他数组中必须有一些元素(使用
.some()
) - 其中包括当前正在考虑的字符串(使用
.includes()
)
演示:
let array1 = ["Lorem", "ipsum", "dolor", "sit", "amet"],
array2 = ["Lorem", "ipsum"],
keywords = ["ipsum", "dol"];
let compare = (a, k) => k.every(s => a.some(v => v.includes(s)));
console.log(compare(array1, keywords));
console.log(compare(array2, keywords));
文档:
这篇关于比较子字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文