Is there an equivalent for ruby's array[n..m]
in JavaScript?
For example:
>> a = ['a','b','c','d','e','f','g']
>> a[0..2]
=> ['a','b','c']
A-Sharabiani
16.3k16 gold badges106 silver badges125 bronze badges
asked Aug 26, 2010 at 23:08
1
Use the array.slice[begin [, end]]
function.
var a = ['a','b','c','d','e','f','g'];
var sliced = a.slice[0, 3]; //will contain ['a', 'b', 'c']
The last index is non-inclusive; to mimic ruby's behavior you have to increment the end
value. So I guess slice
behaves more like a[m...n]
in ruby.
answered Aug 26, 2010 at 23:11
Vivin PaliathVivin Paliath
92k39 gold badges216 silver badges293 bronze badges
1
The second argument in slice
is optional, too:
var fruits = ['apple','banana','peach','plum','pear'];
var slice1 = fruits.slice[1, 3]; //banana, peach
var slice2 = fruits.slice[3]; //plum, pear
You can also pass a negative number, which selects from the end of the array:
var slice3 = fruits.slice[-3]; //peach, plum, pear
Here's the W3 Schools reference link.
answered Aug 26, 2010 at 23:18
David HoersterDavid Hoerster
28.1k8 gold badges65 silver badges100 bronze badges
1
answered Aug 26, 2010 at 23:13
RobertRobert
20.8k9 gold badges54 silver badges65 bronze badges
1
Ruby and Javascript both have a slice method, but watch out that the second argument to slice in Ruby is the length, but in JavaScript it is the index of the last element:
var shortArray = array.slice[start, end];
answered Aug 26, 2010 at 23:17
DouglasDouglas
35.6k8 gold badges73 silver badges89 bronze badges
Need to get a range of items in an array? Here’s how to use
Array.prototype.slice[]
, which is like String.prototype.substring[]
for arrays.
If you read my article on
JavaScript substrings, then you know I actually prefer Ruby’s syntax over JS, because of Ruby’s ..
range operator:
It may be no surprise, then, that I wish I could use Ruby’s range syntax to select a range inside an array — it’s just so convenient!
In Ruby, ["🏄","🏊","🌴","🍹","🌞"][2..3]
equals ["🌴","🍹"]
.
In JavaScript, we need to use Array.prototype.slice[]
. Here’s how.
“The
slice[]
method returns a shallow copy of a portion of an array into a new array object selected frombegin
toend
[end
not included] wherebegin
andend
represent the index of items in that array. The original array will not be modified.” — MDN Docs
In JavaScript, ["🏄","🏊","🌴","🍹","🌞"].slice[2,4]
will produce our beachy pair of emojis, ["🌴","🍹"]
.
Note that the ending index’s item is not included, so you need to add one to the index compared to how the Ruby range method works.
If you don’t include an ending index, you get the remainder
of the array, so ["🏄","🏊","🌴","🍹","🌞"].slice[2]
results in ["🌴","🍹","🌞"]
.
You can also use negative indices, which count backwards from the end of the array: ["🏄","🏊","🌴","🍹","🌞"].slice[-2]
returns ["🍹","🌞"]
.
Either index provided to slice[]
can be positive, zero, or negative:["🏄","🏊","🌴","🍹","🌞"].slice[-4,3]
makes ["🏊","🌴"]
.
Similarly, ["🏄","🏊","🌴","🍹","🌞"].slice[2,-1]
is ["🌴","🍹"]
.
Here’s
a fun fact —slice[]
can actually be used the same way on strings, in which case it works exactly the same as substring[]
.
[One difference is slice[]
takes negative indices; substring[]
won’t.]
Another common use for slice[]
is shallow copying arrays, which I compare to methods for
deep copying arrays in the following article:
As you probably guessed, given my comparison to the .substring[]
method, slice[]
does not alter the original array.
[Neither substring[]
nor slice[]
will modify the original string.]
The built-in method slice[]
is very useful for
returning a portion of an existing array —a subarray or subsequence inside the original array.
Just remember that the array item at the ending index is not included in the sliced portion of the array, and that negative indices count backwards.
Happy coding! 💯😊🔥😍🖥️