首页 > 解决方案 > 通过具有多个键的另一个对象数组过滤对象数组

问题描述

以下是我的代码。“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
        ));

谢谢

标签: javascriptfilter

解决方案


这样的事情将返回带有 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 一样。


推荐阅读