How to reverse an array in Typescript [Array reverse() method]

In this Typescript tutorial, we will see the array reverse() method in typescript, and how to reverse an array in typescript with different examples. Here are the examples we are going to cover:

  • Typescript reverse array
  • Typescript reverse array not in place
  • Typescript reverse iterate array
  • Typescript reverse array sort
  • Typescript reverse JSON array
  • Typescript reverse loop array

New to array? check out Typescript array

Typescript reverse array

Here we will see what is a reverse method in typescript, also we will see a simple example, of how we can use reverse an array using the reverse method in typescript.

Reverse Method in Typescript

The reverse () in typescript is used to reverse the array and return an array, which is sorted, in reference to the original array.

Syntax

reverse()

For example, we have an array, and we will reverse the array using the reverse Method. In the app.ts file, write the below code:

var array= ["henry", "john","Michel"]
console.log("Reversed array is:" + array.reverse())

To compile the code, run the below command and see the result in the console.

ts-node app.ts
Typescript reverse array
Typescript reverse array

This is what reverse() is, and the syntax of the reverse method.

Read TypeScript Dictionary

Typescript reverse array not in place

Here we will see how we can reverse the array, not in place and is not working.

With reverse(), we will reverse the array with mutation, which means when the reverse method is used on the array, it will return the reverse of the array without mutating or without creating a new array. So here we will see how we can reverse the array without mutating the original array. In simple words, we can say we will print a new reverse array.

In the app.ts file, write the below code:

var num = [1, 2, 3, 4, 5];

console.log(num)

var reverse_num = num.slice().reverse()

console.log("new array:" +reverse_num)

With the slice method, it will create a new array, and then with the reverse method, we reverse the array.

To compile the code, run the below command, and you can see the result in the console.

ts-node app.ts
Typescript reverse array not in place
Typescript reverse array not in place

This is an example of a Typescript reverse array not in place.

typescript reverse iterate array

Here we will see how we can use the reverse() to iterate an array in a reverse way in typescript.

As we know the foreach will iterate the array in the forward direction. To iterate through an array in the backward direction, we will use the reverse method.

In the app.ts file write the below code:

var num = [1, 2, 3, 4, 5];
 
num.slice().reverse()
    .forEach(function(elem) {
            console.log(elem);
        });

To compile the code, run the below command and you can see the result in the console:

ts-node app.ts
typescript reverse iterate array
typescript reverse iterate array

This is an example typescript reverse iterate array.

Read TypeScript Date

Typescript reverse array sort

Here we will see how we can reverse the sorted array in typescript.

For example, we have an array of strings, first, we will sort the array in alphabetical order using sort(), then we will use reverse the array using the reverse method.

const array: string[] = ['ammy', 'celia', 'zeck', 'fany'];
const descSortedReveredArray = array.sort().reverse();
console.log(descSortedReveredArray);

To compile the code, run the below command and you can see the result in the console.

ts-node app.ts
Typescript reverse array sort
Typescript reverse array sort

This is an example of a typescript reverse array sort

Typescript reverse json array

Here we will see how we can reverse JSON array in typescript using the reverse method.

For example, we have a JSON array, and by applying the reverse method we will reverse the array in typescript.

In the app.ts file, write the below code:

var usaActors: any= [
    {first_name: 'Robin', last_name: 'Williams'},
    {first_name: 'Morgan', last_name: 'Freeman'},
    {first_name: 'Tom', last_name: 'Hanks'},
    {first_name: 'Bruise', last_name: 'Wilis'},
    {first_name: 'Brad', last_name: 'Pit'},
  ]
  
  var result= usaActors.reverse();
  console.log(result);

To compile the code, run the below command and you can see the result in the console.

ts-node app.ts
Typescript reverse json array
Typescript reverse json array

This is an example of a Typescript reverse JSON array.

Read TypeScript Map

Typescript reverse loop array

Here we will see how we can see a reverse loop array in typescript using the reverse method.

For example, we will use object.key() and reverse method, in for each to get the reverse array.

The Object.keys() function returns an array of the enumerable string-keyed property names of a given object.

In the app.ts file, write the below code:

var num = [1, 2, 3, 4, 5];

Object.keys(num).reverse()
  .forEach(function (index:any) {
    console.log(num[index]);
  });

To compile the code, run the below command and you can see the output in the console.

ts-node app.ts
typescript reverse loop array
typescript reverse loop array

This is an example of a Typescript reverse loop array.

Conclusion

In this typescript tutorial, we learned all about the reverse method, and how we can use the reverse method. Here is the example we cover:

  • Typescript reverse array
  • Typescript reverse array not in place
  • Typescript reverse iterate array
  • Typescript reverse array sort
  • Typescript reverse JSON array
  • Typescript reverse loop array

You may like the following typescript tutorials:

>