2016-11-22 63 views
4

这里是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 
    } 

} 

请帮忙!

+0

似乎是一个功能会比一类简单。 'function transformData(object){return {// do transformations}}' – azium

回答

0

ES6功能组合destructuring assignmentshorthand property assignment可以简化格式化您的对象。

您可以从参数开始解构,甚至可以重命名变量并添加默认值。

所以对于看起来像这样的输入对象...

{ 
    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

谢谢jpdelatorre,这绝对会帮助我清理这个。所以我可以将它设置为模块导出,然后将其包含并传递参数,它会为我返回一个结构化对象? – jrutter

+0

这应该工作! – jpdelatorre

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

谢谢ZekeDroid - 这正是我期待的帮助和指导! – jrutter

相关问题