TypeScript Arrays - Tutorial Kart
TypeScript Arrays
TypeScript Arrays
TypeScript Arrays are collections of similar type objects. Members of an array are called elements. Elements
of an array could be accessed using their index (position of the element from the first element).
For example, an array of marks of a student can contain marks scored in different subjects.
In this tutorial, we shall learn the following actions of an array :
Declare an Array
Initialize the Array
Read or access elements of Array
Update elements of the Array
TypeScript Array Methods
Declare an Array
An array can be declared like any other variable. Datatype of the array is optional. Following is the syntax :
var array_name[:datatype]
var is the keyword to declare a variable
array_name is the name of the array. We can use this name to reference the array in the program.
datatype is the datatype of the array. number[] , string[] , etc
Some of the examples for TypeScript Array declaration are :
var arr1
var arr2:string
var arr3:int
Initialize the Array
In TypeScript, an array can be initialized during its declaration or separately.
To initialize a variable during its declaration, following is the syntax :
var array_name[:array_data_type] = [element1, element2, . ., elmentN]
To initialize a variable after its declaration, following is the syntax :
var array_name[:array_data_type]
array_name = [element1, element2, . ., elmentN]
Examples are :
var marks1 = [58, 68, 74, 88, 64, 90]
var marks2:number[] = [58, 68, 74, 88, 64, 90]
var marks3:number[]
marks3 = [58, 68, 74, 88, 64, 90]
var students:string[] = ["Roshan", "Aditya", "Anisha", "Midhuna"]
Read or access elements of Array
Use index of elements to access elements of TypeScript Array.
example.ts
var marks:number[] = [58, 68, 74, 88, 64, 90]
console.log(marks[0])
console.log(marks[1])
console.log(marks[2])
console.log(marks[3])
Output
58
68
74
88
Size of the array is 6. Index range is [0, 5].
Update elements of the Array
To update elements of TypeScript array, use assignment operator.
example.ts
var marks:number[] = [58, 68, 74, 88, 64, 90]
marks[0]
marks[1]
marks[2]
marks[3]
=
=
=
=
65
68
73
74
console.log(marks[0])
console.log(marks[1])
console.log(marks[2])
console.log(marks[3])
Output
65
68
73
74
Methods of TypeScript Array
TypeScript provides many methods to manipulate Arrays. We shall go through each of them with examples.
concat() ¨C Concatenates elements or arrays to the array
Array.concat() concatenates the array passed as argument to the array.
example.ts
var marks1:number[] = [58, 68, 74, 88, 64, 90]
var marks2:number[] = [82, 67]
var marks:number[] = marks1.concat(marks2)
console.log(marks)
Output
marks : 58, 68, 74, 88, 64, 90, 82, 67
every() ¨C Applies a function on every element of array and returns a boolean
Array.every() applies a function to every element of the array. every() returns true if all the function calls on
elements return true. If function call on any of the element returns false, every() returns false.
example.ts
var marks:number[] = [58, 68, 74, 88, 64, 90]
function isPassed(element, index, array) {
return (element >= 35)
}
var passed = marks.every(isPassed)
if(passed)
console.log("The student has passed.")
else
console.log("The student has failed")
Output
The student has passed.
forEach() ¨C Applies a function on every element of array
Array.forEach() applies a function to every element of the array. forEach() is used to apply a transformation on
an array.
example.ts
var marks:number[] = [58, 68, 26, 88, 27, 90]
function isPassed(value) {
if(value >= 35) return true
else return false
}
var passA = marks.forEach(isPassed)
console.log(passA)
Output
passA : true, true, false, true, false, true
Conclusion
In this TypeScript Tutorial ¨C TypeScript Arrays, we have learnt how to declare and initialize Arrays, access
elements of the array, modify them, and also different methods TypeScript provides for Arrays.
TypeScript
?
TypeScript Tutorial
?
TypeScript Variables
?
TypeScript if
?
TypeScript if-else
?
TypeScript Switch
?
TypeScript For Loop
?
TypeScript While Loop
?
TypeScript Do-While Loop
?
TypeScript Arrays
?
TypeScript Tuples
?
TypeScript Unions
?
TypeScript Functions
?
TypeScript Anonymous Functions
TypeScript Object Oriented Concepts
?
TypeScript Class
?
TypeScript Inheritance
?
TypeScript Method Overriding
?
TypeScript Interface
TypeScript String Operations
?
TypeScript String Length
?
TypeScript String Split
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- declare two dimensional array in typescript
- declare array with values javascript
- coral programming language reference manual
- declare an arry in typescript
- declaring an empty array in typescript
- declare an array of false in javascript
- declaring array of a class in typescript
- declaring an object javascript
- typescript arrays tutorial kart
- declare array of arrays typescript