TypeScript Arrays - TypeScript

What is TypeScript Arrays?

We have limitations when using variables as mentioned below -

Variables are scalar in nature or in simple terms, it comprises of a single at a time. To store n values in a program n variable declarations are needed. So, variables are not practical when larger collection of values need to be stored.

Variables are allocated memory in a random fashion which makes it difficult to retrieve/read the values in the order of their declaration.

To resolve the issues related to variables, TypeScript introduces the concept of arrays to handle it. Array is a homogenous collection of values. In other words, an array is a collection of values of the same data type. It is a user defined type.

Features of an Array

List of the features of an array are given below −

  • An array declaration will allocate sequential memory blocks.
  • Arrays are static which means that an array once initialized cannot be resized.
  • Each memory block describes an array element.
  • Array elements are recognized by a unique integer called as the index/ subscript of the element.
  • Similar to variables, array should be declared before they are used. Use the var keyword to declare an array.
  • Array initialization refers to populating the array elements.
  • Array element values can be modified or updated but cannot be deleted.

Declaring and Initializing Arrays

Syntax below to declare and initialize an array in Typescript

Syntax

An array declaration without data type is considered to be of the type any. In that scenario, the first element data type is set as the array’s data type.

Let us take an example, a declaration like − var numlist:number[] = [2,4,6,8] will generate an array as given below −

Declaring and Initializing Arrays

The array pointer refers to the starting element by default.

We can declare and initialize the arrays in a single statement. Syntax is given below -

Note − The pair of [] is referred as the dimension of the array.

Accessing Array Elements

We can access array element by placing a subscript after the array name. Syntax is given below -

Example: Simple Array

Upon compiling, it will produce following JavaScript code −

The output of the above code is as follows −

Example: Single statement declaration and initialization

Upon compiling, it will produce following JavaScript code −

Its output is as follows −

Array Object

Array objects are used in creating an Array. The Array constructor can be passed.

A numeric value that depicts the size of the array or

A list of comma separated values.

The following example illustrates on how to create an array using this method.

Example

Upon compiling, it will produce the following JavaScript code.

Its output is as follows −

Example: Array Constructor accepts comma separated values

Upon compiling, it will produce the following JavaScript code −

Its result is as below −

Array Methods

Following are the list of methods of an Array Object alongside their description.

S.No.

Method & Description

1.

concat()

Returns a new array comprised of this array joined with other array(s) and/or value(s).

2.

every()

Returns true if every element in this array satisfies the provided testing function.

3.

filter()

Creates a new array with all of the elements of this array for which the provided filtering function returns true.

4.

forEach()

Calls a function for each element in the array.

5.

indexOf()

Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.

6.

join()

Joins all elements of an array into a string.

7.

lastIndexOf()

Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.

8.

map()

Creates a new array with the results of calling a provided function on every element in this array.

9.

pop()

Removes the last element from an array and returns that element.

10.

push()

Adds one or more elements to the end of an array and returns the new length of the array.

11.

reduce()

Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.

12.

reduceRight()

Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.

13.

reverse()

Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.

14.

shift()

Removes the first element from an array and returns that element.

15.

slice()

Extracts a section of an array and returns a new array.

16.

some()

Returns true if at least one element in this array satisfies the provided testing function.

17.

sort()

Sorts the elements of an array.

18.

splice()

Adds and/or removes elements from an array.

19.

toString()

Returns a string representing the array and its elements.

20.

unshift()

Adds one or more elements to the front of an array and returns the new length of the array.

Array Destructuring

As the name suggests, its refers to breaking up the structure of an entity. TypeScript supports destructuring when used in the circumstances of an array.

Example

Upon compiling, it will produce the following JavaScript code.

Its result is as below −

Array Traversal using for…in loop

The for…in loop is used to traverse through an array.

Array is traversed based on the index of the loop.

Upon compiling, it will produce the below JavaScript code.

The result of the above code is given below −

Arrays in TypeScript

The below concepts in arrays are supported in TypeScript −

S.No.

Concept & Description

1.

Multi-dimensional arrays:

TypeScript supports multidimensional arrays. The simplest form of the multidimensional array is the twodimensional array.

2.

Passing arrays to functions

You can pass to the function a pointer to an array by specifying the array's name without an index.

3.

Return array from functions

Allows a function to return an array

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

TypeScript Topics