Methods in Arrays – Part 2

Arrays topic in TypeScript is divided into following parts:

Below are the methods that are discussed in this section

lastIndexOf map pop
push reduce reduceRight
lastIndexOf

This method returns the last index at which an element can be found. It will return -1 if the element is not found.
Returns the last index of the element.
element: this is the element that we are searching in the array
fromIndex: Index from which we need to start searching backwards. This is optional.
Syntax

arrayName.lastIndexOf(element, [fromIndex]);

Below is a sample code

var users:string[] = ["Akanksha","Anita", "Aarav","Apoorv","Anita","Raj"];
console.log("Original Array: " + users);
console.log(users.lastIndexOf("Anita"));
console.log(users.lastIndexOf("Anil"));

Below is the output of above code

map

This method creates a new array with the results of calling a function on every element in the array.
Syntax

arrayName.map(function);

Below is a sample code

var starter = [10,20,30];
function multiplyByFive (val, index, arr) {
    return val * 5;
}
var result = starter.map(multiplyByFive);
console.log(result);

Below is the output of above code

pop

This method removes the last element of the array and returns it.
Syntax

arrayName.pop();

Below is a sample code

var nums:number[] = [10,20,30];
console.log("Original array: " + nums);
console.log("Removing last element: " + nums.pop());
console.log("Modified array: " + nums);

Below is the output of above code

push

This method adds a new element at the end of the array and returns the length of the array.
Syntax

arrayName.push(element1, element2, .....);

Below is an example

var nums:number[] = [10,20,30];
console.log("Original array: " + nums);
console.log("New length of the array: " + nums.push(40,50));
console.log("Modified array: " + nums);

Below is the output of above code

reduce

This method applies a function simultaneously against two values of an array, from left to right, to reduce the array to a single value.
Syntax

array.reduce(function);

Below is an example

var nums:number[] = [10,20,3];
function multiply(a:number, b:number):number{
    return (a*b);
}
console.log(nums.reduce(multiply));

In the above example, it will first multiply 10 with 20. This will give 200. Then it will multiply 200 with 3, resulting in 600. That is, it will perform the operation in left to right direction.
Below is the output of above code

reduceRight

This method applies a function simultaneously against two values of an array, from right to left, to reduce the array to a single value.
Syntax

array.reduceRight(function);

Below is an example

var nums:number[] = [10,20,300];
function subtract(a:number, b:number):number{
    return (a-b);
}
console.log(nums.reduceRight(subtract));

In this example, first it will subtract 20 from 300, which will return 280. Then it will subtract 10 from 280, resulting in 270. That is, it will perform the operation in right to left direction.
Below is the output of above code

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started