javascript - 通过具有多个键的另一个对象数组过滤对象数组
问题描述
以下是我的代码。“id”将具有相同的值。我想通过多个键过滤数据。过滤器未正常进行。
let myArray = [
{
"id": "#prodstck",
"date": "2018-07-24T16:43:21Z"
},
{
"id": "#prodstck",
"date": "2018-04-24T16:43:42Z"
},
];
let filterArray = [
{
"id": "#prodstck",
"date": "2018-07-24T16:43:21Z"
}
];
const filterFeed = myArray.filter(obj=> filterArray.some((f: any) =>
f.id !== obj.id && f.date !== obj.date
));
谢谢
解决方案
这样的事情将返回带有 16:43:21 时间戳的项目。
let myArray = [
{
"id": "#prodstck",
"date": "2018-07-24T16:43:21Z"
},
{
"id": "#prodstck",
"date": "2018-04-24T16:43:42Z"
},
];
let filterArray = [
{
"id": "#prodstck",
"date": "2018-07-24T16:43:21Z"
}
];
const result = myArray.filter(item => {
return filterArray.some(filterItem => item.id === filterItem.id && item.date === filterItem.date)
})
console.log(result);
编辑:将 .find 更改为 .some 就像原来的 OP 一样。
推荐阅读
- java - 在 Java 中访问不同类的静态成员时避免代码重复
- javascript - Vanilla JS:在模态框内自动播放视频
- django - Django 无法分配数据库关系错误
- jmeter - JMeter:CSS 提取器将错误的 __VIEWSTATE 值传输到登录页面
- r - 如何使用 ggplot2 按降序组织构面?
- vba - 如何自动调整表格行高?
- javascript - 将 map() 限制为仅在由范围组成的数组中的标头列表上运行
- javascript - 标识一个值的 javascript 语句
- php - 如何使用 PHP Wrapper 传递 API 密钥
- java - Android 底页对话框动态内容动画