Home > other >  How to filter a JSON response and get needed needed data from it in react native?
How to filter a JSON response and get needed needed data from it in react native?

Time:09-24

Here is my JSON response.

{"tripDateLineItems": [
    {
        "tripDate": "2021-09-15",
        "totalFare": 337,
        "tripLineItemDetails": [
            {
                "voidType": 0
            }
        ]
    },
    {
        "tripDate": "2021-09-13",
        "totalFare": 540,
        "tripLineItemDetails": [
            {
                "voidType": 0
            },
            {
                "voidType": 1
            }
        ]
    },
    {
        "tripDate": "2021-09-12",
        "totalFare": 270,
        "tripLineItemDetails": [
            {
                "voidType": 1
            }
        ]
    }
    ]}

From the above response I need tripdate value when void type in tripLineItemDetails is zero, even if one of the object in tripLineItemDetails is zero I need that tripdate.

CodePudding user response:

const data = response.tripDateLineItems.filter(obj => obj.tripLineItemDetails.find(o => o.voidType == 0));

//Here you'll receive your data with tripDate of voidType 0
console.log(data);

Hope this works for you.

  • Related