我有两个数组:
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]