Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CoffeeScript defines the range of arrays


May 09, 2021 CoffeeScript


Table of contents


Define the range of arrays

Problem

You want to define the range of an array.

Solution

In CoffeeScript, there are two ways to define the range of array elements.

myArray = [1..10]
# => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
myArray = [1...10]
# => [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

To reverse the scope of an element, you can write it as below.

myLargeArray = [10..1]
# => [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
myLargeArray = [10...1]
# => [ 10, 9, 8, 7, 6, 5, 4, 3, 2 ]

Discuss

Contains scope with ".." T he operator definition, which contains the last value. E xclusion to "..." Operator definition, and the last value is usually ignored.