Hier ist das JSON-Objekt, das ich habe:Was ist der beste Weg, um Daten zu übergeben und ein Objekt zu exportieren?
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;
ich Daten in die durch Requisiten in reagieren-redux würde vorbei, aber auf der Suche für den meisten effiencient Weg, um das Objekt zu creat.
Ich habe versucht, erstellen ES6-Klassen und die JSON nach der Formatierung in die richtige Struktur zurück. Die Daten, die ich erhalte, haben nicht dasselbe Format wie das, das ich wie oben an eine API senden muss.
Ich schrieb diese Klasse, die zu funktionieren scheint - aber nicht sicher, ob es der beste Weg nach vorne ist?
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
}
}
Bitte helfen!
wie eine Funktion einfacher als eine Klasse sein würde. 'function transformData (object) {return {// mach Transformationen}}' – azium