【问题标题】:the better way of wring the same value in computed在计算中拧出相同值的更好方法
【发布时间】:2022-11-23 04:11:30
【问题描述】:

我有可以正常运行的代码,我怎样才能更好地编写这段代码

if (!this.isThis) {
    return [                    
        { label: this.$t('profile'), value: this.acc.AccountNumber, id: 'ProfileNumber' },
    ]
} else {
    return [
        { label: this.$t('market'), value: this.acc.label, id: 'market' },
        { label: this.$t('profile'), value: this.acc.AccountNumber, id: 'ProfileNumber' },
        { label: this.$t('account'), value: this.acc.profile, id: 'account' }
    ]
}

我可以使用一些更好的 js 代码来处理这个问题,上面的工作但是有更好的方法来编写

【问题讨论】:

  • imo,这很简单。也许删除else,因为它无论如何都会在return上退出。这就是eslint规则会做的:eslint.org/docs/latest/rules/no-else-return
  • 我赞赏您优化代码的愿望,使它更苗条且更易于维护。请记住,优化太多了。 (但事实并非如此。)

标签: javascript vue.js


【解决方案1】:

鉴于它们共享相同的中间元素,您可以这样重写它:

return [
  ...this.isThis ? [{ label: this.$t('market'), value: this.acc.label, id: 'market' }] : [],
  { label: this.$t('profile'), value: this.acc.AccountNumber, id: 'ProfileNumber' },
  ...this.isThis ? [{ label: this.$t('account'), value: this.acc.profile, id: 'account' }] : [],
]

【讨论】:

    【解决方案2】:

    如果你只有这个 if else case 你可以删除 else :

    如果您的条件有效,那么它将返回 ifs 值

    默认情况下(else)它可以在没有 else 块的情况下返回你的 else 值。

    像这样

    if (!this.isThis) {
    return [                    
        { label: this.$t('profile'), value: 
      this.acc.AccountNumber, id: 
      'ProfileNumber' }
    ]} 
    
    return [
        { label: this.$t('market'), value: this.acc.label, id: 'market' },
        { label: this.$t('profile'), value: this.acc.AccountNumber, id: 'ProfileNumber' },
        { label: this.$t('account'), value: this.acc.profile, id: 'account' }
    ]
    

    【讨论】:

      【解决方案3】:

      @安德鲁公园,

      也许,甚至更小:

      return [
              { label: this.$t('profile'), 
                value: this.acc.AccountNumber, 
                id: 'ProfileNumber' },
              ...this.isThis ? [{ label: this.$t('market'), 
                                  value: this.acc.label, 
                                  id: 'market' },
                                { label: this.$t('account'), 
                                  value: this.acc.profile, 
                                  id: 'account' }
                               ] : [],
            ]
      

      【讨论】:

        【解决方案4】:

        条件真值数组元素可以按常规添加短路,然后过滤:

        return [
            this.isThis && { label: this.$t('market'), value: this.acc.label, id: 'market' },
            { label: this.$t('profile'), value: this.acc.AccountNumber, id: 'ProfileNumber' },
            this.isThis && { label: this.$t('account'), value: this.acc.profile, id: 'account' }
        ].filter(Boolean)
        

        它仍然可能更冗长,但使用条件 push 更容易阅读。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-11-29
          • 2012-05-25
          • 1970-01-01
          • 2012-08-02
          • 2011-01-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多