TypeScript Array slice()

In this chapter, we will explore the slice() method for arrays in TypeScript. This method is a built-in function that returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified. Understanding how to use slice() is useful for extracting parts of an array without altering the original array.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

2. Syntax

array.slice(start?, end?);

Parameters

  • start (optional): Zero-based index at which to start extraction. A negative index can be used, indicating an offset from the end of the sequence. If omitted, slice begins from index 0.
  • end (optional): Zero-based index before which to end extraction. slice extracts up to but not including end. A negative index can be used, indicating an offset from the end of the sequence. If omitted, slice extracts through the end of the sequence (array.length).

Return Value

The method returns a new array containing the extracted elements.

3. Examples

Let's look at some examples to understand how slice() works in TypeScript.

Example 1: Basic Usage

In this example, we use slice() to extract a portion of an array of numbers.

let numbers: number[] = [1, 2, 3, 4, 5];
let slicedNumbers = numbers.slice(1, 3);
console.log(slicedNumbers); // Output: [2, 3]
console.log(numbers);       // Output: [1, 2, 3, 4, 5]

Example 2: Omitting the End Parameter

In this example, we use slice() to extract elements from the start index to the end of the array.

let numbers: number[] = [1, 2, 3, 4, 5];
let slicedNumbers = numbers.slice(2);
console.log(slicedNumbers); // Output: [3, 4, 5]
console.log(numbers);       // Output: [1, 2, 3, 4, 5]

Example 3: Using Negative Indices

In this example, we use slice() with negative indices to extract elements from the end of the array.

let numbers: number[] = [1, 2, 3, 4, 5];
let slicedNumbers = numbers.slice(-3, -1);
console.log(slicedNumbers); // Output: [3, 4]
console.log(numbers);       // Output: [1, 2, 3, 4, 5]

Example 4: Extracting the Entire Array

In this example, we use slice() to extract the entire array.

let numbers: number[] = [1, 2, 3, 4, 5];
let slicedNumbers = numbers.slice();
console.log(slicedNumbers); // Output: [1, 2, 3, 4, 5]
console.log(numbers);       // Output: [1, 2, 3, 4, 5]

Example 5: Slicing a String Array

In this example, we use slice() to extract a portion of an array of strings.

let fruits: string[] = ["apple", "banana", "mango", "orange", "kiwi"];
let slicedFruits = fruits.slice(1, 4);
console.log(slicedFruits); // Output: ["banana", "mango", "orange"]
console.log(fruits);       // Output: ["apple", "banana", "mango", "orange", "kiwi"]

Example 6: Slicing an Array of Objects

In this example, we use slice() to extract a portion of an array of objects.

interface Person {
  name: string;
  age: number;
}

let people: Person[] = [
  { name: "Ravi", age: 25 },
  { name: "Ankit", age: 30 },
  { name: "Priya", age: 28 },
  { name: "John", age: 22 },
];

let slicedPeople = people.slice(1, 3);
console.log(slicedPeople);
// Output:
// [ { name: 'Ankit', age: 30 }, { name: 'Priya', age: 28 } ]
console.log(people);
// Output:
// [ { name: 'Ravi', age: 25 }, { name: 'Ankit', age: 30 }, { name: 'Priya', age: 28 }, { name: 'John', age: 22 } ]

4. Conclusion

In this chapter, we explored the slice() method for arrays in TypeScript, which is used to return a shallow copy of a portion of an array into a new array object selected from start to end. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use slice() effectively can help in various array manipulation tasks in TypeScript, especially when you need to extract parts of an array without altering the original array.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare