title: Use property value as key in map statement without intermediate object assignment
body: <p>I have an array objects that look like this:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;id&quot;: &quot;id&quot;,
      &quot;events&quot;: [{
          &quot;status&quot;: &quot;DELETED&quot;,
          &quot;timestamp&quot;: 0,
          &quot;properties&quot;: {
              &quot;key&quot;: &quot;value&quot;
          }
      }],
      &quot;additionalProperty&quot;: &quot;someValue&quot;
  }
  </code></pre>
  <p>I want to format this array so that the final representation looks like this:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;id&quot;: [{
          &quot;status&quot;: &quot;DELETED&quot;,
          &quot;timestamp&quot;: 0
      }]
  }
  </code></pre>
  <p>Multiple objects would look as follows:</p>
  <pre><code>{
      &quot;id&quot;: [{
          &quot;status&quot;: &quot;DELETED&quot;,
          &quot;timestamp&quot;: 0
      }],
      &quot;id2&quot;: [{
          &quot;status&quot;: &quot;CREATED&quot;,
          &quot;timestamp&quot;: 0
      }]
  }
  </code></pre>
  <p>And I have the following interface in typescript:</p>
  <pre class="lang-js prettyprint-override"><code>interface Response {
      [id: string]: {
          status: string
          timestamp: number
      }[]
  }
  </code></pre>
  <p>How can I iterate over the objects and format each object to satisfy the interface? I've tried using the map function, but creates an array of objects:</p>
  <pre class="lang-js prettyprint-override"><code>return obj.map(o =&gt; ({
      [o.id]: o.events.map(e =&gt; {
          delete e.properties
          return e
      })
  }))
  </code></pre>
  <p>However this gives the error:</p>
  <blockquote>
  <p>Type '{ [x: string]: { status: string; timestamp: number; }[]; }[]' is not assignable to type 'Response'.
  Index signature is missing in type '{ [x: string]: { status: string; timestamp: number; }[]; }[]'</p>
  </blockquote>
  <p>I have managed to solve it using <code>forEach</code> instead, but is it possible to do without the intermediate object?</p>
  <pre class="lang-js prettyprint-override"><code>private formatResponse(packages: PackageDocument[], packageId?: string): PackageHistoryResponse {
      const response: Response = {}
      obj.forEach(o =&gt; response[o.id] = o.events.map(e =&gt; ({ status: e.status, timestamp: e.timestamp })))
      return response
  }
  </code></pre>
  <p><strong>Full reproducible code</strong></p>
  <p>Confirmed running using ts-node.</p>
  <pre class="lang-js prettyprint-override"><code>let someObjects = [{
      id: &quot;id&quot;,
      events: [{
          status: &quot;DELETED&quot;,
          timestamp: 0,
          properties: {
              key: &quot;value&quot;
          }
      }],
      additionalProperty: &quot;someValue&quot;
  }, {
      id: &quot;id2&quot;,
      events: [{
          status: &quot;CREATED&quot;,
          timestamp: 1,
          properties: {
              key: &quot;value&quot;
          }
      }],
      additionalProperty: &quot;someValue&quot;
  }]
  
  interface ResponseInterface {
      [id: string]: {
          status: string
          timestamp: number
      }[]
  }
  
  console.log(&quot;What I have achieved with a one-line expression&quot;)
  console.log(JSON.stringify(someObjects.map(o =&gt; ({
      [o.id]: o.events.map(e =&gt; {
          return { status: e.status, timestamp: e.timestamp }
      })
  })), null, ' '))
  
  console.log(&quot;What I want (preferably in the same fashion as above)&quot;)
  console.log(JSON.stringify(formatResponse(someObjects), null, ' '))
  
  function formatResponse(objects: any[]): ResponseInterface {
      const response: ResponseInterface = {}
      objects.forEach(o =&gt; response[o.id] = o.events.map((e: { status: any; timestamp: any }) =&gt; ({ status: e.status, timestamp: e.timestamp })))
      return response
  }
  </code></pre>