CodesJava

Easy learning with example program codes

TypeScript array object tutorial


TypeScript array object:

A TypeScript Array object represents a collection of elements of the same type.

TypeScript Array Object Properties:

Property Description
constructor Returns a reference to the array function that created the object.
length It reflects the number of elements in an array.
prototype It allows us to add properties and methods to an object.

TypeScript Array Object Methods:

Method Description
concat() Returns a new array comprised of this array joined with other array(s) and/or value(s).
every() Returns true if every element in this array satisfies the provided testing function.
filter() Creates a new array with all of the elements of this array for which the provided filtering function returns true.
forEach() Calls a function for each element in the array.
indexOf() Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
join() Joins all elements of an array into a string.
lastIndexOf() Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
map() Creates a new array with the results of calling a provided function on every element in this array.
pop() Removes the last element from an array and returns that element.
push() Adds one or more elements to the end of an array and returns the new length of the array.
reduce() Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
reduceRight() Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
reverse() Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first.
shift() Removes the first element from an array and returns that element.
slice() Extracts a section of an array and returns a new array.
some() Returns true if at least one element in this array satisfies the provided testing function.
toSource() Represents the source code of an object
sort() Represents the source code of an object
splice() Adds and/or removes elements from an array.
toString() Returns a string representing the array and its elements.
unshift() Adds one or more elements to the front of an array and returns the new length of the array.

 

How to create TypeScript array object:

  • By using array literals.
  • By using Array() constructor.

By using array literals:

Using an array literal is the simplest way to create a TypeScript Array.

Syntax:

var array1:string[] = ["element1","element2","elementN"];

Example:

function arrayTest(names:string):void{
  for (i=0;i<names.length;i++){  
    console.log(names[i]); 
  }
}
var names:string[] = ["Bharat","Sahdev","Richi","Harish","Bharti","Deepika","Shaveta"];
arrayTest(names);

Try it:

JS Bin on jsbin.com

By using Array() constructor:

We can create a TypeScript Array object by using Array() constructor.

Syntax:

var array1 = new Array(“element1”, “element2”, “elementN”);

Example:

function arrayTest(names:string):void{
  for (i=0;i<names.length;i++){  
    console.log(names[i]); 
  }
}
var names:string[] = new Array("Jai","Vivek","Mahesh","Narender","Vishal","Hemant");
arrayTest(names);

Try it:

JS Bin on jsbin.com

TypeScript Tutorial

TypeScript Overview.
Typescript vs Javascript.
Typescript let vs var.
TypeScript Hello World.
Typescript Data Types.
Typescript Variable Declaration.
TypeScript operators list.
TypeScript Arithmetic Operators.
TypeScript Comparison Operators.
TypeScript Bitwise Operators.
Typescript Logical Operators.
TypeScript Assignment Operators.
Typescript Conditional Operator.
Typescript String Operator.
Typescript typeof Operator.
TypeScript control statements.
TypeScript Switch Statement.
TypeScript for loop.
TypeScript for in loop.
TypeScript while loop.
TypeScript do while loop.
Typescript for loop break.
TypeScript for loop continue.
Typescript function.
Typescript returning function.
Typescript parameterized function.
Typescript function optional parameter.
Typescript rest parameters.
Typescript default parameters.
Typescript anonymous function.
Typescript function constructor.
Typescript Arrow/Lambda function.
Typescript function overloading.
TypeScript number object.
TypeScript string object.
TypeScript boolean object.
TypeScript date object.
TypeScript math object.
TypeScript array object.
TypeScript tuples.
TypeScript union type.
TypeScript interface.
TypeScript interface inheritance.
TypeScript interface multiple inheritance.
TypeScript class.
Typescript class inheritance.
Typescript class interface.
TypeScript abstract class.
TypeScript object.
TypeScript duck typing.
TypeScript NameSpace.
TypeScript Multi-file namespaces.
TypeScript module.
TypeScript ambient module.


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap