遍历对象数组以过滤特定数据

问题描述 投票:1回答:1

我正在尝试通过API调用过滤特定的数据。对象不是可迭代的,我尝试了百万种不同的方式来获取所需的特定数据。

所以...我需要过滤掉tip_money并仅返回该数据而不是完整响应。

这里是电话

router.get('/', auth, (req, res) => {
    try {
        apiInstance.listPayments(opts).then(function (payments) {
            res.send(payments)
        })
    } catch (error) {
        console.error(error)
    }
});

这是响应(部分):

{
    "payments": [
        {
            "id": "rt6Q8LL3XrCLGltl3bBhazMF",
            "created_at": "2019-11-12T23:34:03.012Z",
            "updated_at": "2019-11-12T23:53:45.481Z",
            "amount_money": {
                "amount": 13080,
                "currency": "USD"
            },
            "total_money": {
                "amount": 14047,
                "currency": "USD"
            },
            "processing_fee": [
                {
                    "effective_at": "2019-11-13T01:53:44.000Z",
                    "type": "INITIAL",
                    "amount_money": {
                        "amount": 375,
                        "currency": "USD"
                    }
                }
            ],
            "status": "COMPLETED",
            "source_type": "CARD",
            "card_details": {
                "status": "CAPTURED",
                "card": {
                    "card_brand": "VISA",
                    "last_4": "9215",
                    "exp_month": 7,
                    "exp_year": 2023,
                    "cardholder_name": "DE SETT/TIMOTHY A ",
                    "fingerprint": "sq-1-ufSaSRoMCL2sCJ1Dt6Se9iJ3hfH5iBSNU1O8tR2lW3plPkdX-C5_ej-UNolIXt-KUg",
                    "bin": "404658"
                },
                "entry_method": "EMV",
                "cvv_status": "CVV_NOT_CHECKED",
                "avs_status": "AVS_NOT_CHECKED",
                "auth_result_code": "009208",
                "application_identifier": "A0000000031010",
                "application_name": "CITI VISA",
                "application_cryptogram": "6b7992ab9fba75fe",
                "verification_method": "SIGNATURE",
                "verification_results": "UNKNOWN",
                "statement_description": "SQ *PULQUERIA"
            },
            "location_id": "8Z57ETHMDAT8T",
            "order_id": "JQUZ9UTfq8uvFDwtcSuEGxyeV",
            "employee_id": "DrU-af4--DVfMCkl2z73"
        },
        {
            "id": "1KTHzXVunvkdCoWcNvCHbsMF",
            "created_at": "2019-11-12T23:34:18.530Z",
            "updated_at": "2019-11-12T23:53:38.793Z",
            "amount_money": {
                "amount": 2050,
                "currency": "USD"
            },
            "tip_money": {
                "amount": 369,
                "currency": "USD"
            },
            "total_money": {
                "amount": 2601,
                "currency": "USD"
            },
            "processing_fee": [
                {
                    "effective_at": "2019-11-13T01:53:36.000Z",
                    "type": "INITIAL",
                    "amount_money": {
                        "amount": 78,
                        "currency": "USD"
                    }
                }
            ],
            "status": "COMPLETED",
            "source_type": "CARD",
            "card_details": {
                "status": "CAPTURED",
                "card": {
                    "card_brand": "VISA",
                    "last_4": "0977",
                    "exp_month": 5,
                    "exp_year": 2023,
                    "cardholder_name": "CHAN/PEGGY ",
                    "fingerprint": "sq-1-GCWgavx3rREjbTAvc5E-dYvcqaUNkPgi2ZitN9uUikW0GlDvMReWFjo1Aa8mDlbjQg",
                    "bin": "438857"
                },
                "entry_method": "EMV",
                "cvv_status": "CVV_NOT_CHECKED",
                "avs_status": "AVS_NOT_CHECKED",
                "auth_result_code": "02558C",
                "application_identifier": "A0000000031010",
                "application_name": "CHASE VISA",
                "application_cryptogram": "9ebaad24f5812335",
                "verification_method": "SIGNATURE",
                "verification_results": "UNKNOWN",
                "statement_description": "SQ *PULQUERIA"
            },
            "location_id": "8Z57ETHMDAT8T",
            "order_id": "hGX0BpVnxUVKy6olo8b9pz9eV",
            "employee_id": "DrU-af4--DVfMCkl2z73"
        },

朝正确方向提供的任何帮助将不胜感激!

javascript
1个回答
0
投票

看看类似的东西是否对您有用

router.get('/tips', auth, (req, res) => {
    try {
        apiInstance.listPayments(opts).then(function (payments) {
            const tips = payments.map(payment => payment.tip_money).filter(tip => tip != undefined);
            res.send(tips);
        })
    } catch (error) {
        console.error(error)
    }
});

如果您还希望端点返回未定义的提示,请在最后删除.filter,因为似乎所有的付款都没有提示。

© www.soinside.com 2019 - 2024. All rights reserved.