title: Format Array of Objects into a 2D array
body: <p>I have an array of objects and I would like to get it into a format used by react-csv (2D array). The 2D should look like a nested loop, something like</p>
  <p><a href="https://i.stack.imgur.com/41cuj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/41cuj.png" alt="Image of data in spreadsheet program" /></a></p>
  <p>and the end result should be like</p>
  <pre><code>[
    [&quot;Name&quot;, &quot;Message&quot;, &quot;Column&quot;, &quot;configDataType&quot;,&quot;dbDataType&quot;],
    [&quot;STAGE&quot;, &quot;Columns present in source config but not in the database.&quot;, &quot;TEST&quot;],
    [&quot;&quot;, &quot;&quot;, &quot;TEST1&quot;],
    [&quot;&quot;, &quot;&quot;, &quot;TEST2&quot;],
    [&quot;&quot;, &quot;Columns present in database but not in the source config.&quot;, &quot;&quot;],
    [&quot;&quot;, &quot;Columns with datatype mismatch.&quot;, &quot;LAST_NAME&quot;, &quot;varchar(50)&quot;, &quot;varchar(40)&quot;],
    [&quot;&quot;, &quot;&quot;, &quot;FIRST_NAME&quot;, &quot;varchar(50)&quot;, &quot;varchar(40)&quot;],
    [&quot;RAW&quot;, &quot;Column sets are identical.&quot;],
    [&quot;LAST&quot;, &quot;Column sets are identical.&quot;],
    [&quot;HIST&quot;, &quot;Column sets are identical.&quot;],
    [&quot;CORE&quot;, &quot;Column sets are identical.&quot;],
    [&quot;ADDR&quot;, &quot;Column sets are identical.&quot;],
    [&quot;CONFIG&quot;, &quot;Column sets are identical.&quot;],
    [&quot;ACTION&quot;, &quot;Column sets are identical.&quot;],
  ]
  </code></pre>
  <p>This is what I have done so far, but I am not able to get the intended result. Please advise me on what I am missing.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const data = {
    "STAGE": [{
        "infoLevel": "error",
        "message": "Columns present in source config but not in the database.",
        "columns": [],
        "columnsName": [
          "TEST",
          "TEST1",
          "TEST2"
        ],
        "type": "String"
      },
      {
        "infoLevel": "error",
        "message": "Columns present in database but not in the source config.",
        "columns": [],
        "columnsName": [],
        "type": "String"
      },
      {
        "infoLevel": "error",
        "message": "Columns with datatype mismatch.",
        "columns": [{
          "name": "LAST_NAME",
          "configDatatype": "varchar(50)",
          "dbDatatype": "varchar(40)"
        }, {
          "name": "FIRST_NAME",
          "configDatatype": "varchar(50)",
          "dbDatatype": "varchar(40)"
        }],
        "columnsName": [],
        "type": "Table"
      }
    ],
    "RAW": [{
      "infoLevel": "success",
      "message": "Column sets are identical.",
      "columns": [],
      "columnsName": [],
      "type": "String"
    }],
    "LAST": [{
      "infoLevel": "success",
      "message": "Column sets are identical.",
      "columns": [],
      "columnsName": [],
      "type": "String"
    }],
    "HIST": [{
      "infoLevel": "success",
      "message": "Column sets are identical.",
      "columns": [],
      "columnsName": [],
      "type": "String"
    }],
    "CORE": [{
      "infoLevel": "success",
      "message": "Column sets are identical.",
      "columns": [],
      "columnsName": [],
      "type": "String"
    }],
    "ADDR": [{
      "infoLevel": "success",
      "message": "Column sets are identical.",
      "columns": [],
      "columnsName": [],
      "type": "String"
    }],
    "TRAN": [{
      "infoLevel": "success",
      "message": "Column sets are identical.",
      "columns": [],
      "columnsName": [],
      "type": "String"
    }],
    "CONFIG": [{
      "infoLevel": "success",
      "message": "Column sets are identical.",
      "columns": [],
      "columnsName": [],
      "type": "String"
    }],
    "ACTION": [{
      "infoLevel": "success",
      "message": "Column sets are identical.",
      "columns": [],
      "columnsName": [],
      "type": "String"
    }]
  }
  
  const res = _.flattenDeep(Object.keys(data).map(i =&gt; {
    return data[i].map(j =&gt; {
      return {
        ...j,
        tableName: i
      }
    })
  }))
  
  console.log(res.map(i =&gt; {
    return [
      i.tableName,
      i.message,
      ...((i.columns.length &gt; 0 ? i.columns : i.columnsName).map(j =&gt; typeof j === 'string' ? j : j.name)),
      ..._.compact(((i.columns.length &gt; 0 ? i.columns : i.columnsName).map(j =&gt; typeof j === 'string' ? '' : j.configDatatype))),
      ...(((i.columns.length &gt; 0 ? i.columns : i.columnsName).map(j =&gt; typeof j === 'string' ? '' : j.dbDatatype)))
    ]
  }))</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>