Sometimes, we want to get range of items with JavaScript array.
In this article, we’ll look at how to get range of items with JavaScript array.
How to get range of items with JavaScript array?
To get range of items with JavaScript array, we use the slice
method.
For instance, we write
const fruits = ["apple", "banana", "peach", "plum", "pear"];
const slice1 = fruits.slice(1, 3);
to call fruits.slice
with index 1 and 3 to return an array with the items in fruits
between indexes 1 and 2.
Conclusion
To get range of items with JavaScript array, we use the slice
method.