🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Definition
- Syntax
- Examples
- 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 index0.end(optional): Zero-based index before which to end extraction.sliceextracts up to but not includingend. 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment