title: JavaScript - Flatten Array of Nested Objects
body: <p>I am working with an array of objects and need to flatten and re-assign the data set into an array of single objects using the object values as the new object keys.</p>
  
  <p>Incoming Data:</p>
  
  <pre><code>results = [
    {
      surveyValues: [
        { id: 135, name: 'First Name', value: 'Jim' },
        { id: 136, name: 'Last Name', value: 'Jones' },
        { id: 137, name: 'City', value: 'Fredsburg' },
      ],
    },
    {
      surveyValues: [
        { id: 135, name: 'First Name', value: 'Greg' },
        { id: 136, name: 'Last Name', value: 'Jones' },
        { id: 137, name: 'City', value: 'Waverly' },
      ],
    },
  ];
  </code></pre>
  
  <p>Desired Result:</p>
  
  <pre><code>output = [
    {
      id: 1, FirstName: 'Jim', LastName: 'Jones', City: 'Fredsburg',
    },
    {
      id: 2, FirstName: 'Greg', LastName: 'Jones', City: 'Waverly',
    },
  ];
  </code></pre>
  
  <p>My current attempt gets me to placing the updated key/value pairs in the correct order, but creates a huge array of single objects.</p>
  
  <p>Current Code:</p>
  
  <pre><code>const results = [];
  
  surveyResults.map(s =&gt; s.surveyValues)
    .forEach(s =&gt; Object.entries(s)
    .forEach(([key, value]) =&gt; {
      Object.entries(value)
        .forEach(([k, v]) =&gt; {
          if (k === 'name') {
            results.push({
              id: value.id,
              [v]: value.value.toString(),
            });
          }
      });
  }));
  </code></pre>
  
  <p>Current Code Output:</p>
  
  <pre><code>[
    {
      id: 135, FirstName: 'Jim',
    },
    {
      id: 136, LastName: 'Jones',
    },
    {
      id: 137, City: 'Fredsburg',
    },
    {
      id: 135, FirstName: 'Greg',
    },
    {
      id: 136, LastName: 'Jones',
    },
    {
      id: 137, City: 'Waverly',
    },
  ]
  </code></pre>
  
  <p>What am I missing and why is my code not created the new arrays of objects as desired?</p>