I have var ar = [1, 2, 3, 4, 5]
and want some function getSubarray(array, fromIndex, toIndex)
, that result of call getSubarray(ar, 1, 3)
is new array [2, 3, 4]
.
Advertisement
Answer
Take a look at Array.slice(begin, end)
const ar = [1, 2, 3, 4, 5]; // slice from 1..3 - add 1 as the end index is not included const ar2 = ar.slice(1, 3 + 1); console.log(ar2);