An array is a data structure that stores a collection of elements and are stored in contiguous memory locations.
example:
1 2 3 4 |
var days:string[]; days = ["sunday","monday","tuesday","wednesday"] console.log(days[0]); //print sunday console.log(days[1]); //print monday |
- every array index position start with 0. so the first element value will be days[0] as shown in above example.
- Similarly, it you want to fetch 2nd position element, the index will be 1 and the value will be days[1]
Table summarising some common TypeScript array methods and their descriptions:
Method | Description |
push(element1, element2, …, elementX) | Adds one or more elements to the end of the array. |
pop() | Removes the last element of the array and returns it. |
unshift(element1, element2, …, elementX) | Adds one or more elements to the beginning of the array. |
shift() | Removes the first element of the array and returns it. |
splice(start, deleteCount, item1, item2, …) | Adds and/or removes elements from the array. |
sort(compareFunction?) | Sorts the elements of an array in place and returns the array. The compare function is an optional parameter that determines the sort order. |
reverse() | Reverses the elements of an array in place and returns the array. |
concat(array2, array3, …, arrayX) | Joins two or more arrays and returns a new array that contains all the elements. |
slice(start, end?) | Returns a shallow copy of a portion of an array into a new array object. The start and end parameters are optional and specify the index range to extract. |
indexOf(searchElement, fromIndex?) | Returns the first index at which a given element can be found in the array, or -1 if it is not present. The fromIndex parameter is optional and specifies the starting index for the search. |
Note : these are just a few examples. TypeScript arrays have many more methods.
Example code for all array methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
let cars = ['tesla', 'bmw', 'tata']; // push() cars.push('audi', 'honda'); console.log(cars); // outputs: ['tesla', 'bmw', 'tata', 'audi', 'honda'] // pop() let last = cars.pop(); console.log(cars); // outputs: ['tesla', 'bmw', 'tata', 'audi'] console.log(last); // outputs: 'honda' // unshift() cars.unshift('merc', 'maruti'); console.log(cars); // outputs: ['merc', 'maruti', 'tesla', 'bmw', 'tata', 'audi'] // shift() let first = cars.shift(); console.log(cars); // outputs: ['maruti', 'tesla', 'bmw', 'tata', 'audi'] console.log(first); // outputs: 'merc' // splice() cars.splice(1, 2, 'mango', 'toyota'); console.log(cars); // outputs: ['maruti', 'mango', 'toyota', 'tata', 'audi'] // sort() cars.sort(); console.log(cars); // outputs: [ 'audi', 'mango', 'maruti', 'tata', 'toyota' ] // reverse() cars.reverse(); console.log(cars); // outputs: [ 'toyota', 'tata', 'maruti', 'mango', 'audi' ] // concat() let citrus = ['lemon', 'lime']; let allcars = cars.concat(citrus); console.log(allcars); // outputs: ['toyota', 'tata', 'maruti', 'mango', 'audi', 'lemon', 'lime'] // slice() let tropicalcars = allcars.slice(0, 4); console.log(tropicalcars); // outputs: [ 'toyota', 'tata', 'maruti', 'mango' ] |