| by Arround The Web | No comments

Loop Through Object in Reverse Order Using JavaScript

An object is an entity that stores information in a key-value pair. Objects are either iterated in forward or reverse order based on keys and values. Use the Object’s static methods “Object.keys()” or “Object.values()” to extract keys or values of objects, apply the “reverse()” method to reverse the key-value pairs, and then finally apply “forEach()” loop to iterate over the array.

This article will illustrate the procedure for traversing objects in reverse order using JavaScript.

How to Loop Through Objects in Reverse Order Using JavaScript?

For the iterating objects in reverse order, use the two approaches:

    • Reverse order loop based on keys.
    • Reverse order loop based on values.

Let’s examine both approaches individually!

How to Loop Through Objects in Reverse Order Based on Object Keys?

To traverse the Object in reverse order based on the object’s keys, follow three steps:

    • Use the “Object’s” static method called “Object.keys()”: It takes an object as an argument and returns the array of the object’s keys.
    • Apply the “reverse()” method: It will reverse the order of the object’s keys.
    • Finally, apply the “forEach()” method to loop through the object.

Example

First, create an object “info” with key-value pairs:

const info = {
 Name: 'John',
 Age: '24',
 ContactNo: '09345237816',
};

 
Get the keys of the object using the “Object.keys()” method and reverse them by calling the “reverse()” method and store them in a variable “reverseBaseonKeys”:

const reverseBaseonKeys = Object.keys(info).reverse();

 
Finally, traverse the reversed object keys using the “forEach()” method:

reverseBaseonKeys.forEach(key => {
 console.log(key, info[key]);
});

 
Output


The above output indicates that the object keys with their corresponding values are successfully traversed in print on the console in reverse order.

How to Loop Through Objects in Reverse Order Based on Object Values?

There is another approach for looping through objects in reverse order based on the object’s values. To traverse the Object in reverse order based on the object’s values, follow the below-given three steps:

    • Use the “Object’s” static method called “Object.values()”: It takes an object as an argument. It returns the array of the object’s values.
    • Apply the “reverse()” method, which will reverse the order of the object’s values.
    • Finally, apply the “forEach()” method to loop through the object.

Example

Here, use the same Object “info” and get the values of the object “info” using the “Object.values()” method and reverse them by calling the “reverse()” method and finally, store the resultant array in a variable “reverseBaseonKeys”:

const reverseBasedonValues = Object.values(info).reverse();

 
Traverse the reversed object values using the “forEach()” method:

reverseBasedonValues.forEach(value => {
 console.log(value, info[value]);
});

 
Output


The above output shows the values of the object in reverse order.

Conclusion

To loop through the object in reverse order, use the Object’s static methods “Object.keys()” or “Object.values()” to extract keys or values of objects, reverse then using the “reverse()” method and then finally apply “forEach()” loop to iterate over the array. This article illustrated the procedure for traversing objects in reverse order based on keys and values using JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply