這裏是JSON對象,我有:什麼是傳入數據和導出對象的最佳方式?
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;
我會被傳遞數據到這個通過反應,終極版道具,而是尋找最effiencient的方式來創造的對象。
我一直在尋找創建ES6類並返回JSON後格式化到正確的結構。我收到的數據與我需要提交給API的格式不同。
我寫了這個類,這似乎工作 - 但不知道它是否是最好的方法呢?
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
}
}
請幫忙!
似乎是一個功能會比一類簡單。 'function transformData(object){return {// do transformations}}' – azium