agha hamza
agha hamza

Reputation: 13

How to JSON.stringify multiple objects returned from Loop

Here is my mvc code, I want to add multiple travelers objects to travelers array which are generated in a loop and then JSON.stringify them,

return amadeus.booking.flightOrders.post(
        JSON.stringify({
          'data':{
            'type': 'flight-order',
            'flightOffers': [response.data.flightOffers[0]],  
           'travelers':[{
              "id": 1,
              "name": {
                "firstName": req.body.firstname,
                "lastName": req.body.lastname
              },
              "gender": req.body.gender,
              "contact": {
                "emailAddress": req.body.emailaddress,
                "phones": [{
                  "deviceType": req.body.devicetype,
                  "countryCallingCode": req.body.countrycallingcode,
                  "number": req.body.number
                }]
              },
              "documents": [{
                "documentType": req.body.documentype,
                "birthPlace": req.body.birthplace,
                "issuanceLocation": req.body.issuancelocation,
                "issuanceDate": req.body.issuancedate,
                "number": req.body.p_number,
                "expiryDate": req.body.expirydate,
                "issuanceCountry": req.body.issuancecountry,
                "validityCountry": req.body.validitycountry,
                "nationality": req.body.nationality,
                "holder": true
              }]
            }]
        } 
      })
      );

I there a simple way to achieve that?

Upvotes: 0

Views: 685

Answers (1)

Metabolic
Metabolic

Reputation: 2914

JSON.stringify converts a JS object to a string. It does not control or require any specifications for the object's structure in general.

In your case, you can simply add multiple data objects to your travelers' array e.g

return amadeus.booking.flightOrders.post(
    JSON.stringify({
      'data':{
        'type': 'flight-order',
        'flightOffers': [response.data.flightOffers[0]],  
       'travelers':[{
          "id": 1,
          "name": {
            "firstName": req.body.firstname,
            "lastName": req.body.lastname
          },
          "gender": req.body.gender,
          "contact": {
            "emailAddress": req.body.emailaddress,
            "phones": [{
              "deviceType": req.body.devicetype,
              "countryCallingCode": req.body.countrycallingcode,
              "number": req.body.number
            }]
          },
          "documents": [{
            "documentType": req.body.documentype,
            "birthPlace": req.body.birthplace,
            "issuanceLocation": req.body.issuancelocation,
            "issuanceDate": req.body.issuancedate,
            "number": req.body.p_number,
            "expiryDate": req.body.expirydate,
            "issuanceCountry": req.body.issuancecountry,
            "validityCountry": req.body.validitycountry,
            "nationality": req.body.nationality,
            "holder": true
          }]
        },{
          "id": 2,
          "name": {
            "firstName": req.body.firstname,
            "lastName": req.body.lastname
          },
          "gender": req.body.gender,
          "contact": {
            "emailAddress": req.body.emailaddress,
            "phones": [{
              "deviceType": req.body.devicetype,
              "countryCallingCode": req.body.countrycallingcode,
              "number": req.body.number
            }]
          },
          "documents": [{
            "documentType": req.body.documentype,
            "birthPlace": req.body.birthplace,
            "issuanceLocation": req.body.issuancelocation,
            "issuanceDate": req.body.issuancedate,
            "number": req.body.p_number,
            "expiryDate": req.body.expirydate,
            "issuanceCountry": req.body.issuancecountry,
            "validityCountry": req.body.validitycountry,
            "nationality": req.body.nationality,
            "holder": true
          }]
        },{
          "id": 3,
           .
           .
           .
        },{
          "id": 4,
           .
           .
           .
        }]
      } 
    })
  );

and JSON.stringify will convert whole object to string.

To loop on data and add the travelers array, one approach will be:

const travelers = [];
for(let i=0;i<10;i++){

   travelers.push({});
} 

and then

return amadeus.booking.flightOrders.post(
    JSON.stringify({
      'data':{
        'type': 'flight-order',
        'flightOffers': [response.data.flightOffers[0]],  
       'travelers': travelers
      } 
    })
  );

Upvotes: 1

Related Questions