0

I am gathering different values from a series of sequential forms and I am using spreads to pass this information along each step. The issue is that I am struggling to create the data format I wish. How should I structure the spread to the following?

I am trying to create this:

{
   [
      "Your details": {
         "name": "value",
         "email": "value",
         "mobile": "value",
      },
      "Payment details": {
         "cardnumber": "value",
         "cardname": "value",
         "expire": "value",
         "security": "value",
      },
      "Billing address": {
         "cardnumber": "value",
         "cardname": "value",
         "expire": "value",
         "security": "value",
      },
   ]
}

I am using the following spread:

    var value = this.refs.form.getValue()
    if (value) {
        let formValues = { ...this.props.navigation.state.params.form, ['Payment']: { ...value } }
    }

The result is:

{
   "name": "value",
   "email": "value",
   "mobile": "value",
   "Payment": {
      "cardnumber": "value",
      "cardname": "value",
      "expire": "value",
      "security": "value",
   },
   "Billing address": {
      "cardnumber": "value",
      "cardname": "value",
      "expire": "value",
      "security": "value",
   },
}

Thank you in advance.

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Diego Oriani
  • 1,647
  • 5
  • 22
  • 31
  • 1
    It's a little unclear what problem you're having. Could you paste the result of the `let formValues = { ... }` statement so we can see how it differs from what you want to do? – Andy Sep 10 '18 at 18:14
  • 1
    We can't help you if you don't tell us what `this.props.navigation.state.params.form` and `value` are. But your code is clearly creating a property called `Payment`, not `Payment details`, for one thing. Also, the "this" you say you're trying to create is invalid, you have an array initializer inside an object initializer without any property name. – T.J. Crowder Sep 10 '18 at 18:14
  • Thank you. I have now added the result. `this.props.navigation.state.form` is where I retrieve the data from the previous form. `value` is the combination of all the values extracted from the fields of the current form being submited. Its all under one object `{name:value, email: value, mobile:value}` – Diego Oriani Sep 10 '18 at 18:32
  • FWIW, `{['Payment']: { ...value } }` is equivalent to `{'Payment': { ...value } }` or `{Payment: { ...value } }`. No need to use computed properties with string literals. – Felix Kling Sep 10 '18 at 18:39
  • Felix, thank you for your tip. Do you know how can I achieve the result I wish using spreads? – Diego Oriani Sep 10 '18 at 18:42
  • Try without using `spread` at first and simply explicitly assign the properties that you want. – Bergi Sep 10 '18 at 19:06

0 Answers0