What is the best way to transfer data and export an object?

Here is the JSON object that I have:

  const json = {
        "itemDetails": itemsArray,
        "paymentDetails": [{
            "billingAddress": {
                "address": {
                    "address1": billingAddress.address1,
                    "address2": billingAddress.address2,
                    "zipCode": billingCityStateZip.zipCode,
                    "city": billingCityStateZip.city,
                    "type": "US",
                    "addressType": billingAddress.addressType,
                    "stateCode": billingCityStateZip.stateCode,
                    "country": "US"
                },
                "contactInfo": {
                    "dayPhoneNumber": billingPhone,
                    "companyName": billingAddress.companyName
                },
                "personalInfo": {
                    "firstName": billingAddress.firstName,
                    "lastName": billingAddress.firstName
                }
            },
            "cardType": paymentDetails.cctype,
            "cardNumber": paymentDetails.ccnumber,
            "expirationMonth": paymentDetails.ccmonth,
            "expirationYear": paymentDetails.ccyear,
            "cvv": paymentDetails.cvv,
            "type": "creditCard"
        }],
        "shippingDetails": [{
            "shippingAddress": {
                "address": {
                    "address1": shippingAddress.address1,
                    "address2": shippingAddress.address2,
                    "zipCode": shippingCityStateZip.zipCode,
                    "city": shippingCityStateZip.city,
                    "type": "US",
                    "addressType": shippingAddress.addressType,
                    "stateCode": shippingCityStateZip.stateCode,
                    "country": "US"
                },
                "contactInfo": {
                    "email": email.emailAddress,
                    "dayPhoneNumber": shippingPhone,
                    "companyName": shippingAddress.companyName
                },
                "personalInfo": {
                    "firstName": shippingAddress.firstName,
                    "lastName": shippingAddress.lastName
                }
            },
            "unlimitedDetails": {
                "unlimitedFlag": "",
                "unlimitedSKU": "",
                "unlimiteProductId": ""
            },
            "shippingLabelMessages": {
                "labelMessage1": "",
                "labelMessage2": "",
                "labelMessage3": "",
                "labelMessage4": ""
            },
            "itemDetails": itemsArray,
            "type": "hardGoodShippingType"
        }],
        "couponDetails": [],
        "userDetails": {
            "userCheckoutPreferences": {
                "payViaPaypal": "false",
                "payByVouchersOnly": "false"
            },
            "userDateOfBirth": {
                "day": dob.dobDay,
                "month": dob.dobMonth,
                "year": dob.dobYear
            },
            "password": "",
            "emailFlag": "false",
            "userBusinessPartner": {
                "businessPartner": null,
                "businessPartnerNumber": null
            }
        },
        "offerDetails":{
          "responseCode":responseCode
        },
        "webRedirectDetails": {
        }
    }
    return json;

I would pass the data into this through the supports in the reaction-reduction, but I was looking for the most effective way to create an object.

I am creating ES6 classes and returning JSON after formatting it to the correct structure. The data that I receive is not the format that I need to present in the API, as indicated above.

I wrote this class that seems to work - but not sure if its the best way to move forward?

class AddressObject {
  constructor(object) {

    // Create Address
    this.address = {};
    this.address.address1 = object.address1;
    this.address.address2 = object.address2;
    this.address.zipCode = object.zipCode;
    this.address.city = object.city;
    this.address.state = object.state;
    this.address.type = object.type;
    this.address.addressType = object.addressType;
    this.address.country = object.country;

    // Create contactInfo
    this.contactInfo = {};
    this.contactInfo.dayPhoneNumber = object.dayPhoneNumber;
    this.contactInfo.companyName = object.companyName;

    // Create personalInfo
    this.personalInfo = {};
    this.personalInfo.firstName = object.firstName;
    this.personalInfo.lastName = object.lastName;
  }

  getAddress() {
   return this
  }

}

Please, help!

+4
source share
2 answers

ES6 .

.

, , ...

{
  address_1: 'my address line 1',
  address_2: 'my address line 2',
  city: '...',
  contact: {
    firstName: 'John',
    lastName: 'Smith',
    phone: '1234'
  }
}

,

function formatData(data) {
   return {
      address1: data.address_1,
      ...
   }
}

:

function formatData({
   address_1: address1, 
   address_2: address2 = '', 
   city, 
   zipCode, 
   state, 
   contact: { 
       firstName, 
       lastName, 
       phone: dayPhoneNumber 
   }
}) {
   return {
      addressData: {
          address1,
          address2,
          city,
          zipCode,
          state
      },
      contactInfo: {
         companyName,
         dayPhoneNumber
      }
   }
}

...

{
   address1: address1,
   address2: address2
   ...
}
0

, , , , . , , ES6 (, , ), :

function formatDataForAPI(data) {
  const {
    address1,
    address2,
    zipCode,
    city,
    state,
    type,
    addressType,
    country,
    dayPhoneNumber,
    companyName,
    firstName,
    lastName,
  } = data;

  return {
    addressData: {
      address1,
      address2,
      zipCode,
      city,
      state,
      type,
      addressType,
      country,
    },
    contactInfo: {
      dayPhoneNumber,
      companyName,
    },
    personalInfo: {
      firstName,
      lastName,
    },
  };
}

, . , , . , , , , .

0

Source: https://habr.com/ru/post/1661515/


All Articles