title: Node Js - Flatten array of objects
body: <p>I have an array like this</p>
  
  <pre><code>var data = [
      {
          family: "Fam A",
          category: "Cat A",
          products: [
              {
                  name: "Name A1",
                  style: "Style A1"
              },
              {
                  name: "Name A2",
                  style: "Style A2"
              }
          ]
      },
      {
          family: "Fam B",
          category: "Cat B",
          products: [
              {
                  name: "Name B1",
                  style: "Style B1"
              },
              {
                  name: "Name B2",
                  style: "Style B2"
              }
          ]
      }
  ]; 
  </code></pre>
  
  <p>I want to change the inside objects like below</p>
  
  <pre><code>var data = [
      {family:"Fam A", category: "Cat A", name: "Name A1", style:"Style A1"},
      {family:"Fam A", category: "Cat A", name: "Name A2", style:"Style A2"},
      {family:"Fam B", category: "Cat B", name: "Name B1", style:"Style B1"},
      {family:"Fam B", category: "Cat B", name: "Name B2", style:"Style B2"}
  ]  
  </code></pre>
  
  <p>I've tried using map and forEach that I found in other posts but haven't gotten anything to work.</p>
  
  <pre><code>var flattened = data.products.map(x =&gt; Object.assign(x, { productDetails: data.products }))
  
  function getList(data, arrayKey, arrayName) {
      data[arrayKey].forEach(function(element) {
          element[arrayName] = data[arrayName];
      });
      return data[arrayKey];
  }
  </code></pre>
  
  <p>The ideal solution would be able to handle dynamic no of nestings but not necessary to move on.</p>
  
  <p>Thank you in advance for helping a newer dev!</p>