【问题标题】:Coffeescript array destructing ignore some valueCoffeescript 数组解构忽略了一些值
【发布时间】:2017-06-16 22:55:57
【问题描述】:

我在 coffeescript 中使用了数组破坏,它工作正常:

[first, second, third] = [1, 2, 3]
console.log "First:" + first, "Second:" + second, "Third:" + third

但我希望能够忽略数组元素。我想做这样的事情:

[, second, third] = [1, 2, 3]
console.log "Second:" + second, "Third:" + third

这在 EcmaScript 6 中是可能的,但如何在 Coffescript 下实现呢?

【问题讨论】:

  • 轶事,但是当我用回调进行正则表达式替换而不关心“全匹配”参数时,我写了string.replace(/regex/,function(_, capture1, capture2...) { ... })。使用_ 作为“一次性”变量名。也许您可以对[_, second, third] 做同样的事情?不知道这是否有帮助:D
  • 感谢您的评论。这是一个想法,但我更喜欢“真正的”忽略。 :)

标签: javascript arrays coffeescript


【解决方案1】:

我只是找到了如何做到这一点。这可以通过扩展运算符来完成:

[..., second, third] = [1, 2, 3]
console.log "Second:" + second, "Third:" + third

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2013-08-10
    • 2011-01-09
    • 2015-11-26
    相关资源
    最近更新 更多