首页 > 解决方案 > 使用 map 翻译 forEach 语句

问题描述

我正在尝试使用 map 函数重写我使用 forEach 方法编写的代码。但我不确定如何使用我使用地图编写的代码对 order.FunctionStatusList 进行空检查。更具体地说,如何将代码包含在带有 orderAction.OrderList 的 else 语句中。

这是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);
        });

这是带有地图的代码:

            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
            })):[]
        })

编辑这是订单的 json 格式:

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

下面是 order-action json 的外观:

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

}

标签: angulartypescriptrxjs

解决方案


好的,所以您的方向是正确的,您只需else在开发的代码中引入该子句,即在冒号之后传递的属性。

您以一种优雅的方式解决了它,ternary operator允许在if... else其中引入所需条件的内联语句。

我尝试用纯 JS 开发代码,所以请原谅我删除了.this和类型断言。

我在ValidationMessage您提供的代码示例中添加了一个属性。无论哪种方式,再次使用三元运算符,您可以检查它是否为。如果不是,您可以添加''

注意:我假设您是从其他地方获取的,这就是为什么我将它们作为常量添加到函数中 functionTypeCodeactionSelected

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))


推荐阅读