To get a sub-array in TypeScript, you can use the same methods as in JavaScript.
In this example,
numbers
with some values.slice
method to get a sub-array from index 2 to index 4 (exclusive).slice
method returns a new array with the specified range.const numbers: number[] = [1, 2, 3, 4, 5];
const subArray = numbers.slice(2, 4);
console.log('Sub-array:', subArray);
Sub-array: [ 3, 4 ]
In this example,
colors
with some values.slice
method to get a sub-array from index 1 to index 3 (exclusive).slice
method returns a new array with the specified range.const colors: string[] = ['red', 'green', 'blue', 'yellow', 'orange'];
const subArray = colors.slice(1, 3);
console.log('Sub-array:', subArray);
Sub-array: [ 'green', 'blue' ]
In this tutorial, we learned How to get Sub Array in TypeScript language with well detailed examples.