简体   繁体   English

如何在 JavaScript 中使用三元运算符更改函数中的 if else 语句?

[英]How can I change the if else statement in the function with Ternary operator in JavaScript?

How can I change the if else statement in the function with Ternary operator in JavaScript如何在 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 && 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;
}

The code is very inefficient because you find the thing, and then you turn around and find the thing again.代码效率非常低,因为你找到了那个东西,然后你转身又找到了那个东西。 So you end up looping multiple times.所以你最终会循环多次。

To make it more readable, I broke it up into parts.为了让它更具可读性,我把它分成了几部分。 It also loops one time over the object to locate the items with children and the items without children.它还在对象上循环一次以定位有孩子的项目和没有孩子的项目。 There is a ternary operator there to handle with and without.那里有一个三元运算符可以处理和不处理。

The code then determines if it is the child or not and grabs the object.然后代码确定它是否是孩子并抓取对象。

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

I put myself under risk, entering what meanwhile seems to be a battleground of opinions, in order to prove two things.我把自己置于危险之中,同时进入一个似乎是意见的战场,以证明两件事。

  1. Aiming for clean code and readability is not always just for the sake of being missionary or a "I do know better than you" attitude.瞄准干净的代码和可读性并不总是为了成为传教士或“我确实比你更了解”的态度。 It is mostly for one's own peace and health (and the one of the team) and especially for the one's that have to maintain such code soon after and even later.这主要是为了自己(以及团队成员)的安宁和健康,尤其是那些必须在不久之后甚至之后维护此类代码的人。

  2. By refactoring the OP's code towards readability one achieves three things:通过重构OP的代码以提高可读性,可以实现三件事:

  • cutting back the repeated and unnecessary data access to the most necessary ones, and then exactly once.将重复和不必要的数据访问减少到最必要的访问,然后只访问一次。
  • actually making it obvious/readable (thus easier to refactor) what kind of data one is dealing with.实际上使其显而易见/可读(因此更容易重构)正在处理的数据类型。
  • and finally fulfilling the OP's wish of a return-value based on (nested) ternary operators which was not such an easy task to achieve without cleaning up before.并最终实现了 OP 基于(嵌套)三元运算符的返回值的愿望,如果不进行清理,这并不是一项容易实现的任务。

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

Instead of writing: IF condition THEN do_a ELSE do_b you can use ternary operators the same way.而不是写: IF condition THEN do_a ELSE do_b你可以用同样的方式使用三元运算符。

(condition) ? do_a : do_b;

Concrete for your example:具体为您的示例:

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';
}

Btw.顺便提一句。 I recommend to extract methods for do_a or do_b .我建议提取do_ado_b方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM