首页 > 解决方案 > 如何在数组中删除所有其他字符串,但字符串中包含“金属”

问题描述

所以,我正在制作一个 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"]

标签: javascriptarraysstring

解决方案


如果它始终是大写字母 M,请使用filterand includes

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

如果是任何一种情况:

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

推荐阅读