I am trying to take data from one array, group the data by object property, and then send it to a new array. The current data format is as such:
const data = [
{
"dataId": "1",
"tableName": "table1",
"column": "firstHeader",
"rows": [
"a","b","c"
]
},
{
"dataId": "2",
"tableName": "table1",
"column": "secondHeader",
"rows": [
"d","e","f",
]
},
{
"dataId": "3",
"tableName": "table2",
"column": "aNewFirstHeader",
"rows": [
1,2,3
]
}
];
I used the Lodash groupby method, which groups the items in a format that is close to what I am expecting, but not exactly what I want my end result to be. The lodash result is:
[
[
{ "tableName": 'table1',
"dataId": '1',
"header": 'firstHeader',
"rows": ["a","b","c"]
},
{ "tableName": 'table1',
"dataId": '1',
"header": 'secondHeader',
"rows": ["d","e","f"]
}
],
[
{ "tableName": 'table2',
"dataId": '2',
"header": 'aNewFirstHeader',
"rows": [1,2,3] },
]
]
Ultimately, what I am looking for is the end result of this:
[
{
"tableName": "table1",
"column": ["firstHeader", "secondHeader"],
"rows": [["a","b","c"], ["d","e","f"]]
},
{
"tableName": "table2",
"column": ["aNewFirstHeader"],
"rows": [[1,2,3]]
}
]
Is there a different way to use the groupBy method, or perhaps a javascript only approach that would get the end result I am looking for?
groupingcondition?