angular - 使用 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"
}]
}]
}
解决方案
好的,所以您的方向是正确的,您只需else
在开发的代码中引入该子句,即在冒号之后传递的属性。
您以一种优雅的方式解决了它,ternary operator
允许在if... else
其中引入所需条件的内联语句。
我尝试用纯 JS 开发代码,所以请原谅我删除了.this
和类型断言。
我在ValidationMessage
您提供的代码示例中添加了一个属性。无论哪种方式,再次使用三元运算符,您可以检查它是否为真。如果不是,您可以添加''
注意:我假设您是从其他地方获取的,这就是为什么我将它们作为常量添加到函数中
functionTypeCode
。actionSelected
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))
推荐阅读
- javascript - 如何在最后一行的 Handsontable 中自动添加粘贴行?
- python - 来自 Opensea 的 WebScraping Table
- php - 如何在微服务架构 PHP 应用程序中设置 Xdebug,其中后端拆分为多个项目
- c - 如何在C中获得价值宏?
- text - 我的问题是我正在编写代码以在 WhatsApp 上发送带有用户登录详细信息的特定短信,但这不起作用我该怎么做?
- java - 对容器类的构造函数调用的单元测试
- react-native - 世博字体不显示自定义字体
- .net - 将基于.net core 5.0的python 3.8安装到Docker
- assembly - MIPS有效地址
- amazon-web-services - 如何获取 AWS CodeCommit 活跃用户数?