是否可以将 N 个元素与 coffeescript splat 匹配?
Is it possible to match N elements with a coffeescript splat?
是否可以指定一个 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]
据我所知,没有办法限制 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]
是否可以指定一个 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]
据我所知,没有办法限制 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]