title: Transform an Array into an Object using .reduce()
body: <p>I am trying to study <code>Array.reduce</code>. And I was given the following task:</p>
  <p>Input data:</p>
  <pre><code>const report = [
    {
      dateOfReport: &quot;11-01-2021&quot;,
      userId: &quot;id1&quot;,
      userMetric: { first_metric: 10, second_metric: 15 },
    },
    {
      dateOfReport: &quot;11-01-2021&quot;,
      userId: &quot;id2&quot;,
      userMetric: { first_metric: 9, second_metric: 14 },
    },
    {
      dateOfReport: &quot;12-01-2021&quot;,
      userId: &quot;id1&quot;,
      userMetric: { first_metric: 11, second_metric: 14 },
    },
    {
      dateOfReport: &quot;12-01-2021&quot;,
      userId: &quot;id2&quot;,
      userMetric: { first_metric: 16, second_metric: 19 },
    },
  ];
  </code></pre>
  <p>And I need to get this data in the output</p>
  <pre><code>const output = [
    {
      dateOfReport: &quot;11-01-2021&quot;,
      id1: { first_metric: 10, second_metric: 15 },
      id2: { first_metric: 9, second_metric: 14 },
    },
    {
      dateOfReport: &quot;12-01-2021&quot;,
      id1: { first_metric: 11, second_metric: 14 },
      id2: { first_metric: 16, second_metric: 19 },
    },
  ];
  </code></pre>
  <p>I tried to write some code, but I have no idea how to do it correctly. How can I solve this problem?</p>
  <p>Code:</p>
  <pre><code> const result = report.reduce((acc, dataItem) =&gt; {
      let outputArray = [];
  
      if (dataItem) {
        outputArray.push({ ...dataItem, date: dataItem.dateOfReport, [dataItem.userId]: dataItem.userMetric });
      }
  
      return outputArray;
    });
  
    return result;
  </code></pre>