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
and the end result should be like
[
["Name", "Message", "Column", "configDataType","dbDataType"],
["STAGE", "Columns present in source config but not in the database.", "TEST"],
["", "", "TEST1"],
["", "", "TEST2"],
["", "Columns present in database but not in the source config.", ""],
["", "Columns with datatype mismatch.", "LAST_NAME", "varchar(50)", "varchar(40)"],
["", "", "FIRST_NAME", "varchar(50)", "varchar(40)"],
["RAW", "Column sets are identical."],
["LAST", "Column sets are identical."],
["HIST", "Column sets are identical."],
["CORE", "Column sets are identical."],
["ADDR", "Column sets are identical."],
["CONFIG", "Column sets are identical."],
["ACTION", "Column sets are identical."],
]
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.
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 => {
return data[i].map(j => {
return {
...j,
tableName: i
}
})
}))
console.log(res.map(i => {
return [
i.tableName,
i.message,
...((i.columns.length > 0 ? i.columns : i.columnsName).map(j => typeof j === 'string' ? j : j.name)),
..._.compact(((i.columns.length > 0 ? i.columns : i.columnsName).map(j => typeof j === 'string' ? '' : j.configDatatype))),
...(((i.columns.length > 0 ? i.columns : i.columnsName).map(j => typeof j === 'string' ? '' : j.dbDatatype)))
]
}))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

TEST1andTEST2are not in thedata. Instead,ArunandAbncare. You may want to amend either your expected result, or yourdata.