[英]How to find multiple elements in Array - Javascript ,ES6
碼:
let names= ["Style","List","Raw"];
let results= names.find(x=> x.includes("s");
console.log(results); //
如何從數組名稱中獲取包含“s”的名稱,目前,我只得到一個元素,但我需要所有出現。
你必須在這種情況下使用filter
,
let names= ["Style","List","Raw"];
let results= names.filter(x => x.includes("s"));
console.log(results); //["List"]
如果您希望它不區分大小寫,請使用以下代碼,
let names= ["Style","List","Raw"];
let results= names.filter(x => x.toLowerCase().includes("s"));
console.log(results); //["Style", "List"]
為了使它處於敏感狀態,我們必須將字符串的字符全部設置為小寫。
使用過濾器而不是find。
let names= ["Style","List","Raw"];
let results= names.filter(x => x.includes("s"));
console.log(results);
但是你也可以使用forEach()方法:
var names = ["Style","List","Raw"];
var results = [];
names.forEach(x => {if (x.includes("s") || x.includes("S")) results.push(x)});
console.log(results); // [ 'Style', 'List' ]
或者如果你喜歡:
names.forEach(x => {if (x.toLowerCase().includes("s")) results.push(x)});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.