To extract part of an array with JavaScript, we can use the JavaScript array slice
method with the start and end index of the array to extract.
For instance, we can write:
const oldArray = [1, 2, 3, 4, 5]
const newArray = oldArray.slice(1, 3);
console.log(newArray)
We call slice
on oldArray
with the start and end indexes respectively.
The end index is excluded from the returned array.
So the items at indexes 1 and 2 will be included in the returned array.
Therefore, newArray
is [2, 3]
.