【问题标题】:How do I remove every other string but strings with "metal" in them in an array如何在数组中删除所有其他字符串,但字符串中包含“金属”
【发布时间】:2019-09-07 01:50:02
【问题描述】:

所以,我正在制作一个 Steam 贸易机器人,我需要帮助移除某人库存中的所有其他物品并且只显示金属。

例如 数组

["Refined Metal", "Refined Metal", "Reclaimed Metal", "Scrap Metal", "Flare Gun", "Strange Shotgun"]

我希望代码去掉其他没有“金属”一词的字符串。

我已经尝试过 array.filter,但我认为这不是我正在做的事情的正确功能

我试过了

var = ["Refined Metal", "Refined Metal", "Reclaimed Metal", "Scrap Metal", "Flare Gun", "Strange Shotgun"]
var = var(array.filter => "Metal")

我不知道如何使用过滤器

我希望它显示: ["Refined Metal", "Refined Metal", "Reclaimed Metal", "Scrap Metal"]

【问题讨论】:

    标签: javascript arrays string


    【解决方案1】:

    如果总是大写 M,请使用 filterincludes

    const metals = array.filter(e => e.includes("Metal"));
    

    如果是两种情况:

    const metals = array.filter(e => /metal/i.test(e));
    

    【讨论】:

    • 谢谢
    • 不用担心@MatinPlays,总是乐于提供帮助。
    猜你喜欢
    • 2020-02-14
    • 2013-09-09
    • 2017-06-06
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 2021-04-28
    相关资源
    最近更新 更多