是否可以将N个元素与coffeescript splat匹配?

时间:2016-11-03 11:34:49

标签: coffeescript

是否可以指定splat应匹配的元素数量?类似的东西:

foo = [1, 2, 3, 4, 5, 6]
[firstThree...(3), fourth, rest...] = foo

console.log firstThree      // [1, 2, 3]
console.log forth           // 4
console.log rest            // [5, 6]

1 个答案:

答案 0 :(得分:1)

据我所知,没有办法增加对splat可以采用的参数数量的限制。

但您可以使用范围(在Loops and Comprehensions Docs中搜索range)在解构分配中获得类似的语法:

foo = [1, 2, 3, 4, 5, 6]
[firstThree, fourth, rest] = [foo[0..2], foo[3], foo[4..-1]]

firstThree
# => [1, 2, 3]
fourth
# => 4
rest            
# => [5, 6]