JavaScript Array: get “range” of items

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.

Leave a Comment