【问题标题】:Linter error when using let and const for values within an array对数组中的值使用 let 和 const 时出现 Linter 错误
【发布时间】:2018-12-04 23:49:02
【问题描述】:

我有一个如下的数组

const [day, month, year] = dateSegments.map(segment => ((segment.length === 1) ? `0${segment}` : `${segment}`))

这一切都很好,直到 'year' 在下面的 if 语句中被重新分配。 “日”和“月”保持不变

 if (year.length === 2) {
   // blah blah blah loads of code not relevant
   year = `${mostLikelyCentury + year}`
 }

const finalDate = `${day}/${month}/${year}`

我的 linter 基本上对重新分配 'year' 感到害怕,我想知道如何将数组中的值分成各种 let 和 const 以避免这种 linting 错误。

任何帮助将不胜感激

【问题讨论】:

    标签: javascript arrays ecmascript-6 constants let


    【解决方案1】:

    只是不要重新分配给year

    const longYear = year.length === 2
      ? (() => {
          // blah blah blah loads of code not relevant
          return `${mostLikelyCentury + year}`
        })()
      : year;
    
    const finalDate = `${day}/${month}/${longYear}`;
    

    或者,只需将const 更改为let

    【讨论】:

      猜你喜欢
      • 2014-10-09
      • 2019-03-09
      • 2021-12-04
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多