rds80
rds80

Reputation: 629

using map to translate forEach statement

I'm trying to rewrite the code that I've written using a forEach method by using the map function. But I'm not sure how to do the null check for order.FunctionStatusList with the code that I've written using the map. More specifically, how do I include the code in the else statement with orderAction.OrderList.

Here is the code with the 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);
        });

Here is the code with the 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 Here is the json format for the order:

{
    "OrderId": "1",
    "FunctionStatusList": [{
        "FunctionTypeCode": "1",
        "AvailableActions": [{
            "ActionLabel": "1",
            "ActionValue": "1"
        }]
    }]
 }

Here is how order-action json should look:

    {
"FunctionTypeCode": "1",
"SelectedAction: "1",
"OrderList": [{
   "OrderId": "1",
   "IsAvailableActionsLoaded": "1",
   "AvailableActions": [{
      "ActionLabel": "1",
      "ActionValue": "1"
   }]
}]

}

Upvotes: 1

Views: 143

Answers (1)

vleonc
vleonc

Reputation: 227

Okay, so you were going through the right direction, and you just had to introduce the else clause in your developed code, that is the properties passed after the colon.

You resolved it in an elegant way, the ternary operator allows to make an inline if... else statement where the desired conditions can be introduced.

I tried and developed the code in plain JS, so excuse me for removing the .this and the type assertions.

I've added a ValidationMessage property to the sample of code you provided. Either way, again, with a ternary operator, you can check if it is truthy. If it isn't you can add ''

Note: I've assumed you are obtaining functionTypeCode and actionSelected from somewhere else, that's why I added them as constants inside the function.

const orderActionFormatter = (input) => {
    const functionTypeCode = 1;
    const actionSelected = 1;
    return input.map(order => {
        return ({
            FunctionTypeCode: functionTypeCode,
            SelectedAction: actionSelected,
            OrderList: order.FunctionStatusList != null ? order.FunctionStatusList.map(orderFunctionStatus => {
                return ({
                    OrderId: order.OrderId,
                    AvailableActions: orderFunctionStatus.AvailableActions,
                    IsAvailableAction: orderFunctionStatus.AvailableActions.length > 0,
                    ValidationMessage: !!orderFunctionStatus.ValidationMessage ? orderFunctionStatus.ValidationMessage : ''
                })
            }) : {
                OrderId: order.OrderId,
                AvailableActions: [],
                IsAvailableAction: false,
                ValidationMessage: ''
            }
        })
    })
}

const input = [{"OrderId":"1","FunctionStatusList":[{"FunctionTypeCode":"1","AvailableActions":[{"ActionLabel":"1","ActionValue":"1"}],"ValidationMessage":"foo bar"}]}];
const inputNullFunctionStatusList = [{"OrderId":"1","FunctionStatusList":null}]

 console.log(orderActionFormatter(input))
 console.log(orderActionFormatter(inputNullFunctionStatusList))

Upvotes: 2

Related Questions