Currently working on extracting data from mongoDB using javascript and React. In order to utilize D3.js effectively, I need to flatten the array.
Presently, my data looks like this:
[{
"location": [ { "createdAt": "2023-04-03T18:27:42.088Z" } ],
"count": 2,
"item": [ { "name": "football" } ]
},{
"location": [ { "createdAt": "2023-04-03T18:27:42.088Z" } ],
"count": 1,
"item": [ { "name": "Baseball" } ]
}]
Desired format for D3.js visualization:
[{
"createdAt": "2023-04-03T18:27:42.088Z",
"count" : 2,
"name": "football"
},{
"createdAt": "2023-04-03T18:27:42.088Z",
"count": 1,
"name": "baseball"
}]
Attempted iterating through objects but encountered some issues:
{
0.item.0.name: "baseball"
0.count: 1
0.location.0.createdAt: "2023-04-03T18:27:42.088Z"
1.etc...
}
Tried using:
console.log(array.flatMap((element) => element).flat()) ;
Unfortunately, this approach did not yield the desired outcome.