In this chapter, we will explore the unshift()
method for arrays in TypeScript. This method is a built-in function that adds one or more elements to the beginning of an array and returns the new length of the array. Understanding how to use unshift()
is useful for managing and modifying arrays, especially when you need to add elements to the start of the array.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The unshift()
method adds one or more elements to the beginning of an array and returns the new length of the array. This method modifies the original array.
2. Syntax
array.unshift(element1, element2, ..., elementN);
Parameters
element1, element2, ..., elementN
: The elements to add to the beginning of the array.
Return Value
The method returns the new length of the array after the elements have been added.
3. Examples
Let's look at some examples to understand how unshift()
works in TypeScript.
Example 1: Basic Usage
In this example, we use unshift()
to add a single element to the beginning of an array.
let numbers: number[] = [2, 3, 4, 5];
let newLength = numbers.unshift(1);
console.log(newLength); // Output: 5
console.log(numbers); // Output: [1, 2, 3, 4, 5]
Example 2: Adding Multiple Elements
In this example, we use unshift()
to add multiple elements to the beginning of an array.
let numbers: number[] = [3, 4, 5];
let newLength = numbers.unshift(1, 2);
console.log(newLength); // Output: 5
console.log(numbers); // Output: [1, 2, 3, 4, 5]
Example 3: Using unshift()
with Strings
In this example, we use unshift()
to add elements to an array of strings.
let fruits: string[] = ["banana", "mango"];
let newLength = fruits.unshift("apple", "orange");
console.log(newLength); // Output: 4
console.log(fruits); // Output: ["apple", "orange", "banana", "mango"]
Example 4: Using unshift()
with Objects
In this example, we use unshift()
to add objects to an array of objects.
interface Person {
name: string;
age: number;
}
let people: Person[] = [
{ name: "Ankit", age: 30 },
{ name: "Priya", age: 28 }
];
let newPerson: Person = { name: "Ravi", age: 25 };
let anotherPerson: Person = { name: "John", age: 22 };
let newLength = people.unshift(newPerson, anotherPerson);
console.log(newLength); // Output: 4
console.log(people);
// Output:
// [ { name: 'Ravi', age: 25 },
// { name: 'John', age: 22 },
// { name: 'Ankit', age: 30 },
// { name: 'Priya', age: 28 } ]
Example 5: Using unshift()
in a Loop
In this example, we use unshift()
in a loop to add elements to an array in reverse order.
let numbers: number[] = [];
for (let i = 5; i >= 1; i--) {
numbers.unshift(i);
}
console.log(numbers); // Output: [1, 2, 3, 4, 5]
4. Conclusion
In this chapter, we explored the unshift()
method for arrays in TypeScript, which is used to add one or more elements to the beginning of an array and return the new length of the array. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use unshift()
effectively can help in various array manipulation tasks in TypeScript, especially when managing and modifying arrays by adding elements to the beginning.
Comments
Post a Comment
Leave Comment