Received the following data from an API response:
const apiResponse = {
"factoryId": "A_0421",
"loss": [
{
"lossType": "Planned Stoppage Time",
"duration": "1111"
},
{
"lossType": "Quality Time",
"duration": "2222"
}
],
"break": [
{
"lossType": "Format",
"duration": "5.749999",
"eventCount": "2"
}
],
"kpi": [
{
"mpl": "16556.598475000053"
}
]
};
Looking for code to check if the API response contains arrays for loss, break, and kpi, and then extract and map values to form fields below.
I've attempted the code below but I'm struggling to access all array objects.
Could someone help me iterate through and patch all values in a more efficient manner?
prepareFormForImport(resp) {
for (let element of resp) {
return {
factoryId: null,
availableTime: {
bankHoliday: element.loss.lossType === 'Bank Holidays' ? element.duration : 0,
breakDownTime: element.loss.lossType === 'Quality Time' ? element.duration : 0
},
operatingTime: {
maintenanceTime: element.break.lossType === 'Format' ? element.duration : 0
},
uom: element.kpi === 'mpl' ? element.mpl : 0
}
}
}