I am trying to load data into ag-grid
so I got the json data like this format:
{"my_data":{"labels":[1,2,3,...], "idx":["idx1", "idx2", ...]}}
I need it to be like this to pass it to the grid:
{"my_data":[{"labels": 1}, {"labels": 2}, ..., {"idx":"idx1"}, {"idx":"idx2"}, ...}]
Is there a fast way to do it this format or I have to loop through the data using reduce
or map
?
desired format example: https://www.ag-grid.com/sample-data/monthlySales.json
I tried to load the data like this
var obj = $.parseJSON(JSON.stringify(data));
gridOptions.api.setRowData(obj.mydata.labels);
Use reduce
will simplify.
const data = { my_data: { labels: [1, 2, 3], idx: ["idx1", "idx2"] } };
const updated = Object.entries(data.my_data).reduce((acc, [key, value]) => {
value.forEach(item => acc.push({ [key]: item }));
return acc;
}, []);
const data_updated = { my_data: updated };
console.log(data_updated);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加