4 years ago
Afaq Arif
7,409 Views
2
The use of variables to store values causes the following restrictions.
Variables are scalar in nature. In simple, a variable declaration can only have a single at a time. This means we need to declare n variables to store n values in a program. The variable’s usage is not practical for a larger amount of values. Variables are assigned memory in random order in a program. Thus, making it difficult to read the values in the order of their declaration.
TypeScript uses the idea of arrays to handle the same. An array is a similar collection of values. In simple, an array is a set of values of the same data type. It is a user-defined type.
Features of an Array
Here is a list of the features of an array.
An array declaration assigns consecutive memory blocks. Arrays are static. You can not change an array after initializing it. Each memory block shows an array element. We identify array elements by a unique integer called the subscript/index of the element. Arrays should be declared before using them, same as variables. We use the var keyword to declare an array. Array initialization refers to populating the array elements. You can update or modify array element values but can not remove them.
Declaring and Initializing Arrays
The following syntax is used to declare an initialize an array in TypeScript.
Syntax
var array_name[:datatype]; //declaration
array_name = [val1,val2,valn..] //initialization
An array declaration without the data type can be considered of any type. Such types of arrays conclude from the data type of the array’s first element during initialization.
For example, a declaration like − var numlist:number[] = [2,4,6,8] will create an array as given below.
The array pointer mentions to the first element by default.
Arrays may be declared and initialized in a single statement . The syntax for the same is −
var array_name[:data type] = [val1,val2…valn]
Note − The pair of [] is called the dimension of the array.
Accessing Array Elements
The array name followed by the subscript is used to refer to an array element. Its syntax is as follows.
array_name[subscript] = value
Example: Simple Array
var alphas:string[];
alphas = ["1","2","3","4"]
console.log(alphas[0]);
console.log(alphas[1]);
The following JavaScript code will show on composing.
//Generated by typescript 1.8.10
var alphas;
alphas = ["1", "2", "3", "4"];
console.log(alphas[0]);
console.log(alphas[1]);
The output of the above code is as follows.
Example: Single statement declaration and initialization
var nums:number[] = [1,2,3,3]
console.log(nums[0]);
console.log(nums[1]);
console.log(nums[2]);
console.log(nums[3]);
The following JavaScript code will run on composing.
//Generated by typescript 1.8.10
var nums = [1, 2, 3, 3];
console.log(nums[0]);
console.log(nums[1]);
console.log(nums[2]);
console.log(nums[3]);
Its output is as follows.
Array Object
We can use an array object to create an array. The Array setup can also pass.
A numeric value that shows the size of the array or A list of comma-separated values.
The following example shows how to create an array using this method.
Example
var arr_names:number[] = new Array(4)
for(var i = 0;i<arr_names.length;i++) {
arr_names[i] = i * 2
console.log(arr_names[i])
}
The following JavaScript code runs on composing.
//Generated by typescript 1.8.10
var arr_names = new Array(4);
for (var i = 0; i < arr_names.length; i++) {
arr_names[i] = i * 2;
console.log(arr_names[i]);
}
Its output is as follows.
Example: Array Constructor accepts comma separated values
var names:string[] = new Array("Mary","Tom","Jack","Jill")
for(var i = 0;i<names.length;i++) {
console.log(names[i])
}
The following JavaScript code runs on composing.
//Generated by typescript 1.8.10
var names = new Array("Mary", "Tom", "Jack", "Jill");
for (var i = 0; i < names.length; i++) {
console.log(names[i]);
}
The above code runs the following output.
Array Methods
A list of the methods of the Array object along with their explanation is given below.
S.No. Method & Description 1. concat() Returns a new array included of this array joined with other arrays (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) to reduce it to a single value.12. reduceRight() Apply a function simultaneously against two values of the array (from right-to-left) 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
This means to destroy the setup of a body. TypeScript supports destructuring when used in the context of an array.
Example
var arr:number[] = [12,13]
var[x,y] = arr
console.log(x)
console.log(y)
The following JavaScript code will be shown on composing.
//Generated by typescript 1.8.10
var arr = [12, 13];
var x = arr[0], y = arr[1];
console.log(x);
console.log(y);
Its output is as follows.
Array Traversal using for…in the loop
You can use the for…in loop to pass through an array.
var j:any;
var nums:number[] = [1001,1002,1003,1004]
for(j in nums) {
console.log(nums[j])
}
The loop performs an index based array traversal.
It shows following JavaScript code on composing.
//Generated by typescript 1.8.10
var j;
var nums = [1001, 1002, 1003, 1004];
for (j in nums) {
console.log(nums[j]);
}
The output of the above code is given below.
Arrays in TypeScript
TypeScript carries the following concepts in arrays.
S.No. Concept & Description 1. Multi-dimensional arrays TypeScript supports multidimensional arrays whose simplest form is the two-dimensional 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
4 years ago
Afaq Arif
7,409 Views
2