gpt4 book ai didi

javascript - 包含具有 "ing"的字符串的过滤器数组?

转载 作者:行者123 更新时间:2023-11-28 14:38:43 27 4
gpt4 key购买 nike

在 Javascript 中,有没有一种方法可以从数组中选择包含某些内容的单词。例如,["swimming", "basketball", "chess", "rowing"],我只想选择包含“-ing”的项目并向它们应用字符串。

"I go" + "swimming", ect.

相反,有没有办法选择不包含该参数的项目。没有“-ing”的单词

"I play" + "basketball", ect

最佳答案

您可以使用:

let arr = ["swimming", "basketball", "chess", "rowing"];
let result = arr.map(s => (s.includes("ing")? "I go ": "I play ") + s);
console.log(result);

或者,您可以使用 Array.prototype.filter() 过滤此数组并对每个数组执行您想要的任何操作。

let arr = ["swimming", "basketball", "chess", "rowing"];

let strWithIng = arr.filter(s => s.includes("ing"));
let strWithoutIng = arr.filter(s => !s.includes("ing"));

console.log(strWithIng);
console.log(strWithoutIng);

关于javascript - 包含具有 "ing"的字符串的过滤器数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48889322/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com