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.
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.
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.
Just like JavaScript, TypeScript also supports arrays. There are two ways to declare an array:
let array_name[:datatype] = [val1,val2,valn..]
let fruits: string[] = ['Apple', 'Orange', 'Banana'];
let array_name: Array = [val1,val2,valn..]
let fruits: Array= ['Apple', 'Orange', 'Banana'];
There are two types of an 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.
let array_name[:datatype];
array_name = [val1,val2,valn..]
let arr:number[]; arr = [1, 2, 3, 4] console.log("Array[0]: " +arr[0]); console.log("Array[1]: " +arr[1]);
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.
let arr_name:datatype[][] = [ [a1,a2,a3], [b1,b2,b3] ];
let arr_name:datatype[initial_array_index][referenced_array_index] = [ [val1,val2,val 3], [v1,v2,v3]];
var mArray:number[][] = [[1,2,3],[5,6,7]] ; console.log(mArray[0][0]); console.log(mArray[0][1]); console.log(mArray[0][2]); console.log(); console.log(mArray[1][0]); console.log(mArray[1][1]); console.log(mArray[1][2]);
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.
let arr_name:datatype[] = new Array(values);
//array by using the Array object. let arr:string[] = new Array("rookienerd","2200","Java","Abhishek"); for(var i = 0;i
let i:any; let arr:string[] = ["rookienerd", "2300", "Java", "Abhishek"]; for(i in arr) { console.log(arr[i]) }
We can pass arrays to functions by specifying the array name without an index.
let arr:string[] = new Array("rookienerd", "2300", "Java", "Abhishek"); //Passing arrays in function function display(arr_values:string[]) { for(let i = 0;i
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.
let arr1 = [ 1, 2, 3]; let arr2 = [ 4, 5, 6]; //Create new array from existing array let copyArray = [...arr1]; console.log("CopiedArray: " +copyArray); //Create new array from existing array with more elements let newArray = [...arr1, 7, 8]; console.log("NewArray: " +newArray); //Create array by merging two arrays let mergedArray = [...arr1, ...arr2]; console.log("MergedArray: " +mergedArray);
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. |