Ad

Updating JSON Array Specific String

The issue I am having is that I am returning a set of users credit cards from stripe. Because Stripe separates the default card or "source" from the cards object, I need to append the respond back to my app. my app has a struct with an isDefault BOOL variable. The app part can't really be changed.

In the JSON code I posted below, I have made some comments with aarows as to where I want the new key: value to go. In a nutshell, if any of the cards in their own {} matches the default card the user has, then I want to insert the key value: "isDefault": true ONLY into the {} that matches the default card.

I have been toying with this for a while and I am stuck. I have used a for loop to search the array for the matching card ID, but can't figure out how to append in the specific {} of the card, its just going into the whole array.

JSON Response with comments:

{
    "cardsData": [
        {
            "id": "card_328746328746",  
            "object": "card",
            "address_city": null,
            "address_country": null,
            "address_line1": null,
            "address_line1_check": null,
            "address_line2": null,
            "address_state": null,
            "address_zip": null,
            "address_zip_check": null,
            "brand": "Discover",
            "country": "US",
            "customer": "cus_djklfhdskljfhs",
            "cvc_check": "pass",
            "dynamic_last4": null,
            "exp_month": 9,
            "exp_year": 2023,
            "fingerprint": "dfhsdkljfsdlkf",
            "funding": "credit",
            "last4": "1117",
            "metadata": {},
            "name": null,
            "tokenization_method": null
        },
        {
            "id": "card_kjdslhfjdhf",  <-- HAPPENS TO BE DEFAULT CARD, NEED TO INSERT HERE
            "object": "card",
            "address_city": null,
            "address_country": null,
            "address_line1": null,
            "address_line1_check": null,
            "address_line2": null,
            "address_state": null,
            "address_zip": null,
            "address_zip_check": null,
            "brand": "Visa",
            "country": "US",
            "customer": "cus_dkljfhdkjfhd",
            "cvc_check": "pass",
            "dynamic_last4": null,
            "exp_month": 4,
            "exp_year": 2054,
            "fingerprint": "dfjsklkjfhsd",
            "funding": "credit",
            "last4": "4242",
            "metadata": {},
            "name": null,
            "tokenization_method": null
            "isDefault": true  <-- THIS CURRENTLY DOES NOT EXIST, THIS IS WHERE I NEED HELP TO GET THIS INSERTED
        }
    ]
}

Node JS/Cloud Function Code:

exports.getCardsTest = functions.https.onRequest((req, res) => {
    if (req.method !== 'POST') {
        return;
    }
    const customerId = req.body.stripeId;
    stripe.customers.retrieve(
        customerId,
        {

        },
        function (err, customer) {
            const cardsData = customer.sources.data;
            const defaultCard = customer.default_source;

            var isDefault = false;  //check of vairable value
            console.log('isDefault before:', isDefault);

            for (var i = 0; i < cardsData.lenth; i++); {
                if (cardsData[i].id == defaultCard);
                isDefault = true;
                cardsData.push({ isDefault });   <--THIS IS WHERE I NEED HELP, I NEED isDefault: true APPENDED TO THE ARRAY STRING {} MATCHING THE KEY VALUE OF THE DEFAULT CARD
            };

            console.log('isDefaultAfter:', isDefault);  // check to make sure IF statement is working

            return res.status(200).send({ cardsData });
        }
    );

});
Ad

Answer

you can update the value in the array like this

for (var i = 0; i < cardsData.length; i++); {
  if (cardsData[i].id === defaultCard) {
    cardsData[i].isDefault = true
  }
}

your if was useless since a part was missing, also you were pushing inside the array of cards, I think that

Ad
source: stackoverflow.com
Ad