TypeScript Arrays
An array is a homogenous collection of similar type of elements which have a contiguous memory location.
An array is a user-defined data type.
An array is a type of data structure where we store the elements of a similar data type. In an array, we can store only a fixed set of elements. We can also use it as an object.
The array is index-based storage, where the first element stored at index 0. The below structure helps to understand the structure of an array.
Characteristics of an Array
- An array stores elements which have the same data type.
- Array elements stored in contiguous memory locations.
- The storage of 2-D array elements is rowed by row in a contiguous memory location.
- Array name represents the address of the starting element.
- The size of an array should be initialized at the declaration time.
- Array size should be a constant expression and not a variable.
- We can retrieve array elements by specifying the element’s corresponding index value.
Advantage
Code Optimization: An array helps to make the code optimized, which increases the speed and performance of the program. It allows us to retrieve or sort the array data more efficiently.
Random access: It provides the ability to access any data of an array in constant time (independent of its position and size). Thus, we can get any data of an array located at any index position directly.
Disadvantage
Size Limit: An array allows us to store only the fixed number of elements. Once the array is declared, we cannot alter its size. Hence, if we want to insert more element than declared, it is not possible.
Array declaration
Just like JavaScript, TypeScript also supports arrays. There are two ways to declare an array:
1. Using square brackets.
Example:
2. Using a generic array type.
Example:
Types of the array in TypeScript
There are two types of an array:
- Single-Dimensional Array
- Multi-Dimensional Array
Single-Dimensional Array
A single-dimensional array is a type of linear array, which contains only one row for storing data. It has a single set of the square bracket (“[]”). We can access its elements either using row or column index.
Syntax
Initialization
Example
Output:
Array[0]: 1 Array[1]: 2
Multi-Dimensional Array
A multi-dimensional array is an array which contains one or more arrays. In the multi-dimensional array, data is stored in a row and column-based index (also known as matrix form). A two-dimensional array (2-D array) is the simplest form of a multi-dimensional array.
Syntax
Initialization
Example
Output:
1 2 3 5 6 7
Array Object
Array objects allow us to store multiple values in a single variable. We can create an array by using the Array object. The Array constructor is used to pass the following arguments for array creation.
- A numeric value which represents the size of an array or
- A list of comma-separated values.
Syntax
Example
Output:
tutoraspire 2200 Java Abhishek
Array Traversal by using a for…in loop
Example
Output:
tutoraspire 2300 Java Abhishek
Passing Arrays to Functions
We can pass arrays to functions by specifying the array name without an index.
Example
Output:
tutoraspire 2300 Java Abhishek
TypeScript Spread operator
The spread operator is used to initialize arrays and objects from another array or object. We can also use it for object de-structuring. It is a part of the ES 6 version.
Example
Output:
CopiedArray: 1,2,3 NewArray: 1,2,3,7,8 MergedArray: 1,2,3,4,5,6
Array Methods
The list of array methods with their description is given below.
SN | Method | Description |
---|---|---|
1. | concat() | It is used to joins two arrays and returns the combined result. |
2. | copyWithin() | It copies a sequence of an element within the array. |
3. | every() | It returns true if every element in the array satisfies the provided testing function. |
4. | fill() | It fills an array with a static value from the specified start to end index. |
5. | indexOf() | It returns the index of the matching element in the array, otherwise -1. |
6. | includes() | It is used to check whether the array contains a certain element or not. |
7. | Join() | It is used to joins all elements of an array into a string. |
8. | lastIndexOf() | It returns the last index of an element in the array. |
9. | Pop() | It is used to removes the last elements of the array. |
10. | Push() | It is used to add new elements to the array. |
11. | reverse() | It is used to reverse the order of an element in the array. |
12. | Shift() | It is used to removes and returns the first element of an array. |
13. | slice() | It returns the section fo an array in the new array. |
14. | sort() | It is used to sort the elements of an array. |
15. | splice() | It is used to add or remove the elements from an array. |
16. | toString() | It returns the string representation of an array. |
17. | unshift() | It is used to add one or more elements to the beginning of an array. |