title: TypeError: Cannot read property 'reduce' of undefined in react
body: <p>I have a form in which I am asking the user to input field values for a couple of fields, storing the field values in an state and displaying the state values in a customised format.  </p>
  
  <p>So, I have a couple of input fields and a submit button: </p>
  
  <pre><code>&lt;button onClick={this.handleSubmit}&gt;Submit&lt;/button&gt;
           {
             this.state.credentials &amp;&amp;
             //&lt;Credentials value={this.state}/&gt;
              &lt;Credentials value={JSON.stringify(this.state, undefined, 2)} /&gt;
           }
  </code></pre>
  
  <p>The Credentials function convert the state of the component in JSON format: </p>
  
  <pre><code>const Credentials = ({value} ) =&gt; {
      return &lt;pre&gt;{formatState(value)}&lt;/pre&gt;;
  }
  </code></pre>
  
  <p>The formatState function will basically manipulate the state values and display them in the way I want:</p>
  
  <pre><code>function formatState(state) {
    console.log("hi")
    console.log(state);
      const output = state.groups.reduce((final, s)=&gt; {
        console.log(output)
        const values = Object.keys(s).reduce((out, o)=&gt; {
          out[o] = s[o].map(k =&gt; Object.values(k))
          return out;
          }, {})
          final =  {...final, ...values}
          return final;
        }, {})
        console.log(output) 
    }
  </code></pre>
  
  <p>The state looks like this:</p>
  
  <pre><code>{
    "groups": [
      {
        "typeA": [
          {
            "name": "abc"
          },
          {
            "number": "13,14"
          }
        ],
        "typeB": [
          {
            "country": "xyz"
          },
          {
            "date1": "2019-05-14"
          }
        ]
      }
    ]
  }
  </code></pre>
  
  <p>But I want the output like this:</p>
  
  <pre><code>groups: {
  "typeA": [[abc],[13,14]],
  "typeB": [[2019-05-14],[xyz]]
  }
  </code></pre>
  
  <p>SO, reduce function is used to convert the state into the following output. But I getting the error : 
  "TypeError: Cannot read property 'reduce' of undefined"</p>
  
  <p>Please can anybody tell me why this is happening.</p>