Iterating through an array of objects to filter specific data

David Clelland

I am trying to get specific data filtered on an API call. The objects are not iterable and I have tried a million different ways to get the specific data I need.

So...I need to filter out tip_money and return only that data instead of the full response.

Here is the call

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

and here is the response (partially):

{
    "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": "",
                    "exp_month": ,
                    "exp_year": ,
                    "cardholder_name": "",
                    "fingerprint": "",
                    "bin": ""
                },
                "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": ""
            },
            "location_id": "",
            "order_id": "",
            "employee_id": "DrU-af4--DVfMCkl2z73"
        },

any help in the right direction would be appreciated!

Shimmy568

See if something like this works for you

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)
    }
});

Remove the .filter at the end if you also want the endpoint to return the undefined tips since it doesn't seem like all the payments have tips on them.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Iterating through an array of Objects - Javascript

Iterating through array of objects to group all data by date

Filter through array of objects

Filter array of specific objects

iterating through array of nested objects with javascript/lodash

Create array of objects by iterating through an observable

Merging some objects by iterating through an array

Iterating through an array of Class::Struct objects

Iterating through an array of objects using for each or for in

TS error on iterating through an array of objects

NullPointerException when iterating through an array of objects

Iterating through an array of objects C++

trouble iterating through array containing objects in java

Iterating through a multidimensional array in a specific way

Selecting objects that belong to a specific class when iterating through list of objects

Filter through array of objects in javascript

PHP blog (iterating through array data)

Filter Array of objects by specific key

Iterating through an objects values

Iterating through lots of objects

Iterating through javascript objects

Iterating through JSON objects

Looping through an array to filter an array of objects

Filter & iterating through stdclass object array with Bigcommerce API

Iterating through array of objects from your angular 4 firebase database

iterating through an array of objects and displaying the items [REACT JS]

Iterating through non uniform of array of objects with Angular ng-repeat

I am having trouble iterating through an array of objects

Javascript object property 'null' when iterating through an array of objects