【发布时间】:2017-04-05 13:00:36
【问题描述】:
这是我拥有的 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;
我会通过 react-redux 中的 props 将数据传递给它,但会寻找最有效的方法来创建对象。
我一直在研究创建 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 } }
标签: javascript json reactjs ecmascript-6 redux