簡體   English   中英

如何在 JavaScript 的數組元素中找到某些字母?

[英]How to find certain letters in array elements in JavaScript?

在 JavaScript 中,如何找到包含某些字母的數組元素?

例如,如何在下面定義find以匹配superphone但不匹配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"));

請注意,這將返回一個數組,以防找到多個匹配項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM