多个 if else 条件的替代方案,使其成为单行 - javascript
alternatives for multiple if else conditions to make it in single line - javascript
在一个函数中,我正在用多个 if else 条件检查几个条件。我想使用三元运算符使其成为单行。我无法对其进行配置,该怎么做
if (!this.$route.params.prdKey) {
this.prodData.prdKey = '';
} else {
this.prodData.prdKey = this.$route.params.prdKey;
}
if (!this.$route.params.version) {
this.prodData.version = 1;
} else {
this.prodData.version = this.$route.params.version;
}
this.pageMode = this.$route.params.pageMode;
this.getProductDetailsList();
if (!this.isPrdAvailable) {
this.getproductList();
} else {
this.loadMoreproducts();
}
我也可以使用任何其他方法。在其他部分,我多次使用这种 if-else 条件检查。所以,我也可以删除它们。
||
可用于在两种可能性之间交替 - 如果左侧为真,则计算结果为真,否则计算结果为右侧。
this.prodData.prdKey = this.$route.params.prdKey || '';
this.prodData.version = this.$route.params.version || 1;
你可以在这里使用两个三元表达式:
this.prodData.prdKey = this.$route.params.prdKey ? this.$route.params.prdKey : '';
this.prodData.version = this.$route.params.version ? this.$route.params.version : 1;
在一个函数中,我正在用多个 if else 条件检查几个条件。我想使用三元运算符使其成为单行。我无法对其进行配置,该怎么做
if (!this.$route.params.prdKey) {
this.prodData.prdKey = '';
} else {
this.prodData.prdKey = this.$route.params.prdKey;
}
if (!this.$route.params.version) {
this.prodData.version = 1;
} else {
this.prodData.version = this.$route.params.version;
}
this.pageMode = this.$route.params.pageMode;
this.getProductDetailsList();
if (!this.isPrdAvailable) {
this.getproductList();
} else {
this.loadMoreproducts();
}
我也可以使用任何其他方法。在其他部分,我多次使用这种 if-else 条件检查。所以,我也可以删除它们。
||
可用于在两种可能性之间交替 - 如果左侧为真,则计算结果为真,否则计算结果为右侧。
this.prodData.prdKey = this.$route.params.prdKey || '';
this.prodData.version = this.$route.params.version || 1;
你可以在这里使用两个三元表达式:
this.prodData.prdKey = this.$route.params.prdKey ? this.$route.params.prdKey : '';
this.prodData.version = this.$route.params.version ? this.$route.params.version : 1;