如何在JavaScript中的数组元素中查找某些字母?
在 JavaScript 中,如何查找包含某些字母的数组元素?
例如,我怎么能确定find在下面的匹配super和phone,但不wood?
var array1 = ["super", "phone", "wood"]
find("h", "n", "e")
message.channel.send(results) // should send phone
我在 discord.js 上做这件事,而且相当新
回答
这应该可以解决问题:
function find(...letters){
var words = ["super", "phone", "wood"]
return words.filter(w => letters.every(l => w.includes(l)));
}
console.log(find("h", "n", "e"));
console.log(find("s", "u"));
console.log(find("o"));
请注意,这将返回一个数组,以防找到多个匹配项。