javascript - 在另一个对象数组中过滤对象数组
问题描述
我有以下array
对象
[
{
weakness: [
"Fire",
"Flying",
"Ice",
"Psychic"
],
id: 1,
type: [
"grass",
"poison"
]
},
{
weight: 28.7,
weakness: [
"Fire",
"Flying",
"Ice",
"Psychic"
],
id: 2,
type: [
"grass",
"poison"
]
},
{
weakness: [
"Fire",
"Psychic",
"Flying",
"Ice"
],
id: 3,
type: [
"grass",
"poison"
]
},
{
weakness: [
"Fire",
"Flying",
"Ice",
"Psychic"
],
id: 3,
type: [
"grass",
"poison"
]
}
]
我想根据类型过滤,例如“草”类型
但是,我无法输入array
进行此操作
我尝试使用过滤器进行过滤,但没有成功
解决方案
有一个包含 4 个对象的数组,最后一个没有草。您可以使用此过滤器获得输出。
let a = [
{
weakness: [
"Fire",
"Flying",
"Ice",
"Psychic"
],
id: 1,
type: [
"grass",
"poison"
]
},
{
weight: 28.7,
weakness: [
"Fire",
"Flying",
"Ice",
"Psychic"
],
id: 2,
type: [
"grass",
"poison"
]
},
{
weakness: [
"Fire",
"Psychic",
"Flying",
"Ice"
],
id: 3,
type: [
"grass",
"poison"
]
},
{
weakness: [
"Fire",
"Flying",
"Ice",
"Psychic"
],
id: 4,
type: [
"poison"
]
}
]
console.log(a.filter(e => e.type.findIndex(e => e === 'grass') > -1))
推荐阅读
- sql - 使用 SQL 进行 12 小时转换
- javascript - 手动触发释放按下/触摸的元素
- angular - Angular 6:在 angular-component-library 中使用时未加载 Bing 地图
- ruby-on-rails - 使用 Action Cable 从模型、rails 调用类方法获取未定义的局部变量或方法
- python - Python:从文件中分割句子
- javascript - 对 express 和 Mongoose 做出正确的错误处理和响应
- .htaccess - 重定向目标 URL 在域后缺少斜杠
- python - While 循环忽略条件(如果,否则),只打印第一个建议的打印选项
- php - 如何忽略来自 WordPress 插件样式表的特定 CSS 代码?
- python - 检查用户创建的列表项是否存在于预制列表中的问题