如何在JavaScript中使用三元运算符更改函数中的if-else语句?
如何在JavaScript中使用三元运算符更改函数中的if-else语句如何在JavaScript中使用三元运算符更改函数中的if-else语句?,javascript,typescript,conditional-operator,Javascript,Typescript,Conditional Operator,如何在JavaScript中使用三元运算符更改函数中的if-else语句 private getProductName(productType: string): string { let productName = 'Product not found'; if(this.deal.packages.find(p => p.isSelected).dealProducts.find(dp => (dp.children.length > 0 &&a
private getProductName(productType: string): string {
let productName = 'Product not found';
if(this.deal.packages.find(p => p.isSelected).dealProducts.find(dp => (dp.children.length > 0 && dp.children[0].product.productTypeCode == productType))){
productName = this.deal.packages.find(p => p.isSelected).dealProducts.find(dp => (dp.children.length > 0 && dp.children[0].product.productTypeCode == productType)).children[0].product.name;
}
else if(this.deal.packages.find(p => p.isSelected).dealProducts.find(dp => (dp.children.length === 0 && dp.product.productTypeCode === productType))){
productName = this.deal.packages.find(p => p.isSelected).dealProducts.find(dp => (dp.children.length === 0 && dp.product.productTypeCode === productType)).product.name;
}
return productName;
}
而不是写:
如果条件然后做a,否则做b,你可以用同样的方法使用三元运算符
状况?答:答
具体举例来说:
private getProductName(productType: string): string {
return (this.deal.packages.find(p => p.isSelected).dealProducts.find(dp => (dp.children.length > 0 && dp.children[0].product.productTypeCode == productType)))
? this.deal.packages.find(p => p.isSelected).dealProducts.find(dp => (dp.children.length > 0 && dp.children[0].product.productTypeCode == productType)).children[0].product.name;
: (this.deal.packages.find(p => p.isSelected).dealProducts.find(dp => (dp.children.length === 0 && dp.product.productTypeCode === productType)))
? this.deal.packages.find(p => p.isSelected).dealProducts.find(dp => (dp.children.length === 0 && dp.product.productTypeCode === productType)).product.name;
: 'Product not found';
}
顺便说一句,我建议为do_a或do_b提取方法。代码效率很低,因为你找到了东西,然后转身又找到了东西。所以你会循环多次 为了让它更具可读性,我把它分成了几个部分。它还会在对象上循环一次,以定位有子项的项目和没有子项的项目。这里有一个三元运算符,可以处理有无 然后,代码确定它是否是子对象并抓取该对象
// Grab the package
var selectedPackageProducts = this.deal.packages.find(p => p.isSelected).dealProducts;
// check to see if the children has the product type or if the parent does (if no children)
const selectedProduct = selectedPackageProducts.find(dp =>
dp.children.length > 0 ?
dp.children[0].product.productTypeCode === productType :
dp.product.productTypeCode === productType)
// If we have children use it, else reference the parent
const productObj = selectedProduct && selectedProduct.children.length ?
selectedProduct.children[0] :
selectedProduct;
// get the product name
const productName = productObj && productObj.product.name
为了证明两件事,我把自己置于危险之中,同时进入了一个似乎是意见的战场 追求干净的代码和可读性并不总是为了传教士或一种我比你更了解的态度。这主要是为了个人的和平与健康,为了团队的和平与健康,尤其是为了那些必须在不久之后甚至以后维护这些代码的人 通过重构OP的代码以提高可读性,可以实现三件事: 将重复和不必要的数据访问减少到最必要的数据访问,然后只进行一次。 实际上,使其变得明显/可读,从而更容易重构正在处理的数据类型。 最终实现了OP基于嵌套三元运算符的返回值的愿望,这在以前没有清理的情况下是不容易实现的。
外观怪异的JavaScriptop可能OP首先希望通过将一些重复访问的引用分配给变量/常量来提供代码的可读性。那么用三元表达式替换if-else子句也就容易多了,所以代码是不可读的,你想进一步降低可读性吗?我说得对吗?为了改进你的代码,我建议:拥有可读的代码比拥有非常紧凑的代码更有价值。还要打断长代码行。创建命名变量而不是长if子句,或者将它们提取到isFooBar*方法。从productName=旁边的代码中提取方法。在顶部修正你的拼写方法。毕竟,引入十进制运算符应该很容易。作为提示:阅读Bobs叔叔的书清洁代码。@nologin这是你的观点。有人可以在白板上写一本书,抹掉整本书,然后重写它来修正一个打字错误。或者有人可以从一开始就优化它,而不是擦除整个白板。上面的代码基本上找到了表示它存在的对象。然后它扔掉它,再次找到它。这不是最好的逻辑。给出如何更好地构造代码的想法是一个答案。很抱歉,你对此感觉不太好。有些时候,一个人提出了一些要求,但最终,有些东西并不是OP真正需要的。你为什么重复OP根本不可读的示例代码?因为它是一个具体问题的具体解决方案。如果是我向我的团队成员提供这样的代码,我会很高兴地要求立即更改这个不可读的一行代码。这不是问题,问题是有很多更好的方法来回答它,而不需要代码反复重复。代码效率很低。您在else部分遗漏了dp.children.length==0,请注意我在if语句和else语句中得到的product.name都不同。@KarimAli um,如果子项大于0。。。。三元组的else部分中的children将为零,将其稍加修改以引用children。我对您的唯一更改是跳过三元组,在selectedProductItem之前添加if。。。。如果你已经找到了什么,为什么selectedProductItem会循环?@epascarello。。。保留三分之一,我想满足问题的要求,并请OP和nologin。关于建议的改进,selectedProductItem会在selectedProducts不存在的情况下进行计算,并有相应的防护措施。这也是一个人可以按照OP的要求编写最终返回值的原因。在这一点上,它只是关于检索产品名称。正如我在帖子下面的评论中所说的,有时候OP所要求的并不是他们真正需要的;但在编辑过程中,它也起到了同样的作用。我在这里使用原创海报已经12年了,我不打算改变。追求干净的代码是一件好事。但它并没有回答这个问题。
private getProductName(productType: string): string {
const defaultProductName = 'Product not found';
const selectedPackageProductList = this.deal.packages
.find(p => p.isSelected).dealProducts;
const selectedProducts = selectedPackageProductList
.find(dp => (dp.children.length > 0 && dp.children[0].product.productTypeCode === productType));
const selectedProductItem = !selectedProducts && selectedPackageProductList
.find(dp => (dp.children.length === 0 && dp.product.productTypeCode === productType));
return selectedProducts
? selectedProducts.children[0].product.name
: (selectedProductItem ? selectedProductItem.product.name : defaultProductName);
}