基于另一个数组联接数组的项



我有两个数组:

const words = ['a man', 'a man who', "hiding"];
const speechWords = ["this", "is", "a", "man", "hiding", "under", "blanket", "a", "man", "who", "lives", "here"];

我想从speechWords内部的words数组中搜索是否有匹配项(单词的匹配顺序(,并返回一个新数组,如下所示:

["this", "is", "a man", "hiding", "under", "blanket", "a man who", "lives", "here"]

注意,如果我们有来自words的匹配短语,我们应该加入speechWords的匹配。

注意:不应更改speechWords的顺序。

我试图在words数组中循环,将单词拆分,并在每个单词中搜索speechWords上的循环,但没有成功。。。

一个选项是用一些符号替换大字符串中的空格,然后插入words数组中的子字符串:

const words = ['a man', 'a man who', "hiding"];
const speechWords = ["this", "is", "a", "man", "hiding", "under", "blanket", "a", "man", "who", "lives", "here"];
s = speechWords.join('@')
for (sub of words)
s = s.replace(sub.replaceAll(' ', '@'), sub)
s = s.split('@')
console.log(s)

用于复制arry

const arr=[1,2,4,6]

常量数据=[…arr,7,9,12]

console.log(数据(//[1,2,4,6,7,9,12]

相关内容

最新更新