JavaScript includes()

説明

檢査陣列是否包含某値
回傳結果為布林値

範例

const arr=["moko", "moko123", "niko", "mmoko"];

//用includes()檢査有沒有
var moko=arr.includes("moko");
console.log(moko)//true

//結合filter()跟includes(),可以篩出有關鍵字的陣列
var mmoko=arr.filter(the => the.includes("moko"));
console.log(mmoko)//["moko", "moko123", "mmoko"]

Leave a Reply

Your email address will not be published. Required fields are marked *