javascript - 如何过滤数组以仅返回白名单中包含至少一个嵌套属性的项目?
问题描述
我实际上要做的是过滤我的 GitHub 问题(通过 REST API),以仅显示包含与我的特定白名单匹配的标签的问题。
const issues = [
{
issue_name: "My issue",
labels: [{ name: "Foo" }, { name: "Bar" }, { name: "Baz" }],
},
{
issue_name: "My issue",
labels: [{ name: "Red" }, { name: "Green" }, { name: "Blue" }],
},
];
const whitelist = ['Baz'];
// How to filter issues to only an issue that has at least one label contained in the whitelist?
const filtered = issues.filter(issue => issue.labels.forEach(label => whitelist.indexOf(label) > - 1);
// ?? not sure of something like this would work?
我被卡住了,因为a)标签名称是嵌套的,b)有多个,所以我不知道如何迭代可能的标签名称以返回过滤的问题列表。
解决方案
最好使用 JS find 来做到这一点:
const filtered = issues.filter(i=> i.labels.find(l => whitelist.find(w => w == l.name)))
推荐阅读
- java - Android ImageButton 事件在第一次单击时不会执行所有操作
- python - Python:如何根据星期几获取数据框中某个范围内的项目数?
- azure - Azure 事件总线接收消息并处理它们的顺序和重复
- vue.js - Wowjs 和 Animate.css 不适用于 VUEJS
- python - 无法在我的 conda 环境中更改 python 的版本
- reactjs - 如何在 React Typescript 项目的地图循环中添加解构道具的类型?
- c - 将输入文件名 (const *file*) 存储到 char 数组
- swift - 在 SwiftUI 中使用自定义 UIKit 类
- mpi - 如果分区的默认时间限制不够,如何在 SLURM 中运行更长的作业?
- java - 您如何修复此代码,以便如果用户输入大写或小写字母,只要它是正确的字母,他们就会被告知它的索引: