javascript 根据从复选框中获得的值从对象数据数组中过滤

uinbv5nw  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(129)

我试图过滤一些记录来自我的API数据,我试图使用过滤器,但我不知道我应该如何做的multipoe项目。
这是我的代码,我有API响应,当我做一个复选框时会发生什么
我的API数据

{
    "requestId": "",
    "totalRecords": 132,
    "status": "SUCCESS",
    "items": [
        {
            "Id": 3489,
            "Status":"Awaiting Funds",
            "amountPaid": {
                "unit": "CAD",
                "value": 10
            },
            "Name": "John Doe"
        },
        {
            "Id": 3508
            "Status":"Awaiting Funds",
            "amountPaid": {
                "unit": "CAD",
                "value": 10
            },
            "Name": "John Doe"
        },
        {
            "Id": 3503,
            "Status":"Awaiting Funds",
            "amountPaid": {
                "unit": "CAD",
                "value": 25
            },
            "Name": "John Sinth"
        }
    ]
}

在我的checkbnox中,我传递了两个复选框值,它们是完整对象,如3508、3503,但现在确定了如何操作
这是我的代码,我做了什么,但我没有得到fileted记录

const parsedData = JSON.parse(JSON.stringify(this.apiData));
                    if (this.multipleSelection.length > 0) {
                        const data = parsedData.filter(function (el)
                        {
                            return el.Id = 3489 and 3503
                        })
                    } else {
                        const data = parsedData;
                    }
gk7wooem

gk7wooem1#

Array.prototype.filter()Array.prototype.includes()结合使用

const data = {
    "requestId": "",
    "totalRecords": 132,
    "status": "SUCCESS",
    "items": [
        {
            "Id": 3489,
            "Status":"Awaiting Funds",
            "amountPaid": {"unit": "CAD","value": 10},
            "Name": "John Doe"
        },{
            "Id": 3508,
            "Status":"Awaiting Funds",
            "amountPaid": {"unit": "CAD","value": 10},
            "Name": "John Doe"
        },{
            "Id": 3503,
            "Status":"Awaiting Funds",
            "amountPaid": {"unit": "CAD","value": 25},
            "Name": "John Sinth"
        }
    ]
};

const ids = [3503, 3508];
const parsedData = data.items.filter((item) => ids.includes(item.Id));

console.log(parsedData)

如果上面的内容还不清楚,下面是一个使用复选框的示例:
x一个一个一个一个x一个一个二个x

相关问题