javascript - 如何在数组中删除所有其他字符串,但字符串中包含“金属”
问题描述
所以,我正在制作一个 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"]
解决方案
如果它始终是大写字母 M,请使用filter
and includes
:
const metals = array.filter(e => e.includes("Metal"));
如果是任何一种情况:
const metals = array.filter(e => /metal/i.test(e));
推荐阅读
- svg - 无法将 SVG 图像上传到任何字形制造商,但在 Inkscape 中正确呈现
- angular - Angular:错误处理 - 拦截器和模式
- python - 如何进行多字段输入
- pointers - NASM ctypes SIMD - 如何访问返回给 ctypes 的 128 位数组?
- google-apps-script - Google 电子表格值使用 Google Apps 脚本推送到表单,但按字母顺序排序
- scala - 火花 2.1.1 在 EMR 上读取/写入红移错误
- html - 网格格式化有问题
- django - 即使在 virtualenv 中,Django 也找不到包
- firebase - Chrome 扩展中的 Firebase 电话号码身份验证
- php - 一个请求中带有 json 的 CakePHP REST API 多部分文件