I have a set of data that needs to be reformatted according to a specific format that i desire. Below is the format of data that I'm receiving.
const recieved = [
{
"name": "1PM Industries Inc ",
"series": [
{
"value": 0.0001,
"name": "2019-08-30"
},
{
"value": 0,
"name": "2019-08-28"
}
]
}
]
What i need to do is iterate through all object property keys "name"
, "series"
, "value"
and change them to "id"
, "data"
, "x"
and "y"
respectively.
Below is the format of data that i want the above data set to be changed.
I need the "name"
to be replaced with "x"
and "value"
should be replaced with "y"
const columns = [
{
"id": "japan",
"data": [
{
"x": "plane",
"y": 45
},
{
"x": "helicopter",
"y": 253
}
]
}
]
I found out that we can access property keys of objects by Object.keys
function formatData(columns) {
columns.map(col => {
})
}
I find myself in really hard situations when it comes to formatting of data. Hope someone could help me with this. Thanks
This should work:
received.map(r => ({
id: r.name,
data: r.series.map(s => ({
x: s.name,
y: s.value
}))
}));
Map over each received object, return a new object. id
of the new object is name
of the received object. data
of new object is a map of series
of old objects converting name
to x
and value
to y
.