javascript - 将过滤器与包含一起使用不会过滤
问题描述
我正在尝试过滤掉一个classes
名为 studentUserProfile 的对象中的对象数组。当我运行下面的代码时,过滤器不会过滤掉任何东西。
对象studentUserProfile
;
{
"_id":"ZgfgbGpMxF5T6pNT8",
"firstName":"Angela",
"classes":[
{
"classId":"3x8cNzzr4DQ4PioM7"
},
{
"classId":"oehC4pWbFLDAjbzvt"
}
]
}
filterIds = ["oehC4pWbFLDAjbzvt", "3x8cNzzr4DQ4PioM7"];
代码
let studentUserProfiles = this.props.studentUserProfiles.map((studentUserProfile) => {
studentUserProfile.classes = studentUserProfile.classes.filter(myClass => filterIds.includes(myClass.classId));
return studentUserProfile;
});
解决方案
仅供参考-所以问题与我如何返回有关studentUserProfile
。
const studentUserProfiles = this.props.studentUserProfiles.map((studentUserProfile) => {
const classes = studentUserProfile.classes.filter(myClass => filterIds.includes(myClass.classId));
if (classes.length >= 1) {
return {
...studentUserProfile,
classes,
};
}
return null;
}).filter(studentUserProfile => studentUserProfile !== null);
推荐阅读
- python - 如何标准化我的数据框,以使我的线图从同一点开始?
- select - 选择文件响应 APDU
- ssl - Apache2 SSL 问题:从 zerossl.com 生成免费 SSL,但找不到密钥文件,那么如何使用 certificate.crt 文件创建它?
- react-native - 无法解析模块@react-navigation/stack
- node.js - 如何在 Web 应用程序中集成 Microsoft Project Dashboard?
- r - 如何在 keras 包中定义 lstm、单位、滞后和批量大小
- android - 自定义视图出现问题,如果在同一布局中添加多个自定义视图
- javascript - 如何按属性自定义排序对象?
- python-3.x - 在 BeautifulSoup 中解析网页时获取不正确的链接
- javascript - 复制到另一个文件夹期间文件中的错误