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.

TypeScript Arrays

Characteristics of an Array

  1. An array stores elements which have the same data type.
  2. Array elements stored in contiguous memory locations.
  3. The storage of 2-D array elements is rowed by row in a contiguous memory location.
  4. Array name represents the address of the starting element.
  5. The size of an array should be initialized at the declaration time.
  6. Array size should be a constant expression and not a variable.
  7. 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:

Using square brackets.

Syntax
let array_name[:datatype] = [val1,val2,valn..]
Example #1
snippet
let fruits: string[] = ['Apple', 'Orange', 'Banana'];

Using a generic array type.

Syntax
let array_name: Array = [val1,val2,valn..]
Example #2
snippet
let fruits: Array = ['Apple', 'Orange', 'Banana'];

Types of the array in TypeScript

There are two types of an array:

  1. Single-Dimensional Array
  2. Multi-Dimensional Array
TypeScript Arrays
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
let array_name[:datatype];

Initialization

Syntax
array_name = [val1,val2,valn..]
Example
snippet
let arr:number[]; 
arr = [1, 2, 3, 4] 
console.log("Array[0]: " +arr[0]); 
console.log("Array[1]: " +arr[1]);
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.

TypeScript Arrays
Syntax
snippet
let arr_name:datatype[][] = [ [a1,a2,a3], [b1,b2,b3] ];

Initialization

Syntax
snippet
let arr_name:datatype[initial_array_index][referenced_array_index] = [ [val1,val2,val 3], [v1,v2,v3]];
Example
snippet
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]);
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
let arr_name:datatype[] = new Array(values);

Example

snippet
//array by using the Array object.
let arr:string[] = new Array("rookienerd","2200","Java","Abhishek");
for(var i = 0;i
Output
rookienerd 2200 Java Abhishek

Array Traversal by using a for...in loop

Example
snippet
let i:any; 
let arr:string[] = ["rookienerd", "2300", "Java", "Abhishek"];
for(i in arr) { 
   console.log(arr[i]) 
}
Output
rookienerd 2300 Java Abhishek

Passing Arrays to Functions

We can pass arrays to functions by specifying the array name without an index.

Example
snippet
let arr:string[] = new Array("rookienerd", "2300", "Java", "Abhishek"); 
//Passing arrays in function
function display(arr_values:string[]) {
   for(let i = 0;i
Output
rookienerd 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
snippet
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);
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.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +