I'm currently in the process of refactoring my code to use the map function instead of forEach. However, I am facing an issue with implementing a null check for order.FunctionStatusList while using map. Specifically, I need guidance on how to handle the else statement when dealing with orderAction.OrderList.
Below is the original code using forEach:
this.orders.forEach((order: Order) => {
let orderAction: OrderAction = new OrderAction();
orderAction.FunctionTypeCode = this.functionTypeCode;
orderAction.SelectedAction = this.actionSelected;
if (order.FunctionStatusList != null) {
order.FunctionStatusList.forEach(orderFunctionStatus: OrderFunctionStatus => {
orderAction.OrderList = [{
OrderId: order.OrderId,
AvailableActions: orderFunctionStatus.AvailableActions,
IsAvailableActions: orderFunctionStatus.AvailableActions.length > 0,
ValidationMessage: OrderFunctionStatus.ValidationMessage
}];
});
}
else {
orderAction.OrderList = [{
OrderId: order.OrderId,
AvailableActions: [],
IsAvailableAction: false,
ValidationMessage: ''
}];
}
this.orderActionList.push(orderAction);
});
And here is the updated code utilizing map:
this.orderActionList = this.orders.map(order => ({
FunctionTypeCode: this.functionTypeCode,
SelectedAction: this.actionSelected,
OrderList: order.FunctionStatusList ? order.FunctionStatusList.map((orderFunctionStatus: OrderFunctionStatus) => ({
OrderId: order.OrderId,
AvailableActions: orderFunctionStatus.AvailableActions,
IsAvailableAction: orderFunctionStatus.AvailableActions.length > 0,
ValidationMessage: orderFunctionStatus.ValidationMessage
})):[]
})
|EDIT| Additionally, see below the json format for the order:
{
"OrderId": "1",
"FunctionStatusList": [{
"FunctionTypeCode": "1",
"AvailableActions": [{
"ActionLabel": "1",
"ActionValue": "1"
}]
}]
}
Lastly, here is the expected JSON structure for order-action:
{
"FunctionTypeCode": "1",
"SelectedAction: "1",
"OrderList": [{
"OrderId": "1",
"IsAvailableActionsLoaded": "1",
"AvailableActions": [{
"ActionLabel": "1",
"ActionValue": "1"
}]
}]
}