title: How to flatten nested objects in Typescript?
body: <p>I am trying to flatten the following object 'raw' into object 'flat'</p>
  <pre><code>raw = [    {
        &quot;id&quot;:&quot;123&quot;,
        &quot;Date&quot;:&quot;12/12/2020&quot;,
        &quot;Type&quot;:{
           &quot;id&quot;:&quot;456&quot;,
           &quot;desc&quot;:&quot;test1&quot;
        }    },    {
        &quot;id&quot;:&quot;124&quot;,
        &quot;Date&quot;:&quot;12/12/2020&quot;,
        &quot;Type&quot;:{
           &quot;id&quot;:&quot;456&quot;,
           &quot;desc&quot;:&quot;test2&quot;
        }    } ]
  </code></pre>
  <pre><code>flat =[   {
        &quot;id&quot;:&quot;123&quot;,
        &quot;Date&quot;:&quot;12/12/2020&quot;,
        &quot;desc&quot;:&quot;test1&quot;
        },    
        {
        &quot;id&quot;:&quot;124&quot;,
        &quot;Date&quot;:&quot;12/12/2020&quot;,
        &quot;desc&quot;:&quot;test2&quot;
        }]
  </code></pre>
  <p>I attempted the following:</p>
  <pre><code>
     let flatData:any = []
      const flattenObject = (obj:any) =&gt; {
        const flattened:any = {}
      
        Object.keys(obj).forEach((key) =&gt; {
          if (typeof obj[key] === 'object' &amp;&amp; obj[key] !== null) {
            Object.assign(flattened, flattenObject(obj[key]))
          } else {
            flattened[key] = obj[key]
          }
        })
        flatData.push(flattened)
        console.log(flattened)
        return flattened
      }
  
  </code></pre>
  <p>Result I get from the code snippet above.</p>
  <p><a href="https://i.stack.imgur.com/4HfwY.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4HfwY.jpg" alt="enter image description here" /></a></p>
  <p>...........................................................................................</p>