优化这段代码 if (this.form.productType != 1) { this.form.orderStatus = ""; this.form.productName = ""; this.form.orderCode = ""; this.form.saleStartDate = ""; this.form.saleEndDate = ""; this.form.playStartDate = ""; this.form.playEndDate = ""; this.form.sessionCode = ""; this.form.showManagementName = ""; } else { this.form.orderStatus = ""; this.form.productName = ""; this.form.orderCode = ""; this.form.saleStartDate = ""; this.form.saleEndDate = ""; this.form.playStartDate = ""; this.form.playEndDate = ""; // this.sessionList =[]; } console.log(this.form, "form", this.sessionList); this.form.salesTime = "sales"; this.selectUsers = []; this.pagination.pageNum = 1; this.pagination.pageSize = 10
时间: 2024-03-01 19:51:22 浏览: 100
可以使用对象解构来简化代码,同时可以将重复的部分提取出来,避免代码的重复:
```
const clearFields = {
orderStatus: "",
productName: "",
orderCode: "",
saleStartDate: "",
saleEndDate: "",
playStartDate: "",
playEndDate: "",
};
if (this.form.productType != 1) {
this.form = { ...this.form, ...clearFields, sessionCode: "", showManagementName: "" };
} else {
this.form = { ...this.form, ...clearFields, sessionList: [] };
}
console.log(this.form, "form", this.sessionList);
this.form.salesTime = "sales";
this.selectUsers = [];
this.pagination.pageNum = 1;
this.pagination.pageSize = 10;
```
这样可以使代码更简洁,易于维护。同时,使用对象解构还可以避免手动把每个字段赋值为空字符串,这样代码的可读性也会更好。
相关问题
memset(&Order, 0 , sizeof(Order)); // 将插入报单信息结构体转换成报单结构体,并传回 strcpy(Order.BrokerID, req.BrokerID); strcpy(Order.InvestorID, req.InvestorID); strcpy(Order.InstrumentID, req.InstrumentID); strcpy(Order.UserID, req.UserID); Order.OrderPriceType = req.OrderPriceType; Order.Direction = req.Direction; strcpy(Order.CombOffsetFlag, req.CombOffsetFlag); strcpy(Order.CombHedgeFlag, req.CombHedgeFlag); Order.LimitPrice = req.LimitPrice; Order.VolumeTotalOriginal = req.VolumeTotalOriginal; Order.TimeCondition = req.TimeCondition; strcpy(Order.GTDDate, req.GTDDate); Order.VolumeCondition = req.VolumeCondition; Order.MinVolume = req.MinVolume; Order.ContingentCondition = req.ContingentCondition; Order.StopPrice = req.StopPrice; Order.ForceCloseReason = req.ForceCloseReason; Order.IsAutoSuspend = req.IsAutoSuspend; strcpy(Order.BusinessUnit, req.BusinessUnit); Order.RequestID = req.RequestID; Order.UserForceClose = req.UserForceClose; Order.IsSwapOrder = req.IsSwapOrder; // 报单的唯一标识 strcpy(Order.OrderRef, req.OrderRef); Order.FrontID = frontId; Order.SessionID = sessionId; // 初始报单状态 Order.OrderStatus = MAN_TAOJIN_OST_Unknown; Order.OrderSubmitStatus = MAN_TAOJIN_OSS_InsertSubmitted;
这段代码是将一个插入报单信息的结构体转换成报单结构体,并将其传回。其中,memset函数用于将Order结构体的所有成员初始化为0。接下来,使用strcpy函数将结构体中的各个成员赋值为插入报单信息结构体中对应的成员值。最后,设置报单结构体的一些默认值,如OrderStatus和OrderSubmitStatus等。需要注意的是,OrderRef是报单的唯一标识,需要保证其唯一性。FrontID和SessionID是用于标识客户端的编号和会话编号,用于区分不同的客户端发出的报单请求。
else if ((Order_leg2_close.OrderStatus == MAN_TAOJIN_OST_Canceled) || (strcmp(Order_leg2_close.OrderRef, "") == 0) || (Order_leg2_close.OrderSubmitStatus == MAN_TAOJIN_OSS_CancelRejected))
这是一个条件判断语句,其中包含三个条件:
1. Order_leg2_close.OrderStatus == MAN_TAOJIN_OST_Canceled:判断变量Order_leg2_close中的OrderStatus是否等于MAN_TAOJIN_OST_Canceled,如果是,则条件成立。
2. strcmp(Order_leg2_close.OrderRef, "") == 0:判断变量Order_leg2_close中的OrderRef是否为空字符串,如果是,则条件成立。
3. Order_leg2_close.OrderSubmitStatus == MAN_TAOJIN_OSS_CancelRejected:判断变量Order_leg2_close中的OrderSubmitStatus是否等于MAN_TAOJIN_OSS_CancelRejected,如果是,则条件成立。
只要三个条件中有一个成立,整个条件判断语句就成立。当整个条件判断语句成立时,会执行其后面的代码块。
阅读全文