After receiving data from the backend, it looks like this:
const fetchResult = {
cmo: 'integrated',
schedule1: '2021-08-12',
schedule2: '2021-09-20',
kendaraan: {},
kubikasi1: 207000,
kubikasi2: 20000,
status_so1: true,
status_so2: false,
}
My desired format is as follows:
const result = [
{
schedule: value,
kubikasi: value,
status_so: true
},
{
schedule: value,
kubikasi: value,
status_so: false
},
]
I aim to convert the JSON data received from the backend into an array of objects and group it according to the expected format. The dynamic nature of data elements like schedule
from the API should be considered.
Here's what I have attempted so far:
for (let i = 1; i <= 4; i++) {
if (cmo["schedule_" + i]) {
data.push({
schedule: cmo["schedule_" + i],
namakendaraan: cmo["namakendaraan" + i],
kendaraan: cmo["kendaraan" + i],
totalCarton: cmo["totalCarton" + i],
tonase: cmo["tonase_" + i],
totalTonaseKendaraan: cmo["totalTonaseKendaraan" + i],
totalPercentaseTonaseOrder: cmo["totalPercentaseTonaseOrder" + i],
kubikasi: cmo["kubikasi_" + i],
totalKubikasiKendaraan: cmo["totalKubikasiKendaraan" + i],
totalPercentaseKubikasiOrder:
cmo["totalPercentaseKubikasiOrder" + i],
nomor_so: cmo["nomor_so_" + i],
status_so: cmo["status_so_" + i],
});
} else {
data.push({
schedule: null,
namakendaraan: null,
kendaraan: null,
totalCarton: null,
tonase: null,
totalTonaseKendaraan: null,
totalPercentaseTonaseOrder: null,
kubikasi: null,
totalKubikasiKendaraan: null,
totalPercentaseKubikasiOrder: null,
nomor_so: null,
status_so: null,
});
}