以下是我的代码。 “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原来的那样。
你有正确的想法,这只是你的some
条件不正确。它应该是:
f.id === obj.id && f.date === obj.date
也就是说,当some
和true
与第二个数组中的一个条目匹配时,你希望filter
返回id
(以便date
保留条目)。
实例:
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
));
console.log(filterFeed);
您需要从回调函数返回并且条件将检查eqality
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(function(obj) {
return filterArray.some(function(f) {
return f.id === obj.id && f.date === obj.date
})
});
console.log(filterFeed)