javascript - 比较两个数组并找出差异
问题描述
所以,基本上我有两个这样的数组
const firstArray = [
{
subId:40,
value:280,
label:'something'
},
{
subId:42,
value:280,
label:'something different'
}
]
和另一个包含视频的数组
const secondary = [
{
chapId: 280,
label:'any video',
},
{
chapId: 280,
label:'another video',
},
{
chapId: 282,
label:'another video',
}
]
现在我想返回在 firstArray firstArray.value === secondary.chapId 中匹配的辅助数组中的所有结果。
结果如下:
const secondary = [
{
chapId: 280,
label:'any video',
},
{
chapId: 280,
label:'another video',
},
]
我用 reduce 试过了,但我没有得到确切的值
我想将下拉列表中的结果数组用于反应组件。
解决方案
尝试一下。
const firstArray = [
{
subId:40,
value:280,
label:'something'
},
{
subId:42,
value:280,
label:'something different'
}
]
const secondary = [
{
chapId: 280,
label:'any video',
},
{
chapId: 280,
label:'another video',
},
{
chapId: 282,
label:'another video',
}
]
let ret = secondary.filter((x) => firstArray.find((y) => x.chapId === y.value));
console.log(ret);
推荐阅读
- javascript - React - 如何处理图像/文件的 403 错误
- awk - sed 匹配一个模式并插入换行符,后跟替换文本
- jsf - 如果触发了 onchange,有没有办法找出按钮单击
- swift - Kotlin 和 swift 全地图导航速度
- android - 滚动视图底部的粘滞按钮
- spring - 使用 spring-boot-admin 通过 HTTP 监控非 spring-boot 应用程序
- python - 在 nifti 文件中保存具有复杂组件的矢量
- android-gradle-plugin - 哪个 gradle 版本与最新的 Android Studio 3.5 一起使用?
- netlogo - 如何一次选择/询问更多品种?
- angular - 使用 Response -> HttpResponse 将项目从 Angular 5 迁移到 8 问题