I have a use case to create a table with dynamic data including the header name. I have tried in many ways to reconstruct the array from the API response, But still unable to develop it.
Receiving Array from Response as below
[
{
"category": "Emergency services",
"code": "RMY 90",
"limit": "9032",
"OT": "4124"
},
{
"category": "Pediatrician",
"code": "RMY 80",
"limit": "1232",
"OT": "9412"
},
{
"category": "Neurology",
"code": "RMY 70",
"limit": "6423",
"OT": "7312"
}
]
Grid Expected array
[
{
"item": "Block Code",
"Emergency services": "RMY 90",
"Pediatrician": "RMY 80",
"Neurology": "RMY 70"
},
{
"item": "Total Capacity",
"Emergency services": "9032",
"Pediatrician": "1232",
"Neurology": "6423"
},
{
"item": "OT Capacity",
"Emergency services": "4124",
"Pediatrician": "9412",
"Neurology": "7312"
}
]
I have tried with Object.keys() and map but couldn't properly construct the array. since the code is in the client system I couldn't share it. Please help me on reconstruct the array.
My code is
const rowList = ["Block Code", "Total Capacity", "OT Capacity"];
let arr = [];
const dumming = rowList?.forEach(item => {
for (const [key, object] of Object.entries(responseData)) {
let x = [];
Object.keys(object)?.forEach(item => {
x.push(object[item]);
});
// console.log('rrrr', x);
}
let val = responseData.map((ned, index) => {
let x = {};
Object.keys(object)?.forEach(item => {
x = ned[item]
});
// let cor = Object.entries(ned).map(([key, leaf]) => key+leaf);
return {
id: `${ index }-${ item }`,
name: item, ...x
};
});
arr.push(val);
});
console.log(arr);
Im not sure this is correct. Got stuck in something
