Last updated: Mar 4, 2024
Reading timeยท3 min
To loop through an object in reverse order:
Object.keys()
method to get an array of the object's keys.reverse()
method to reverse the array.forEach()
method to iterate over the array and access the object's
keys and values in reverse order.const obj = { a: 'one', b: 'two', c: 'three', }; // ๐๏ธ ['c', 'b', 'a'] const reversedKeys = Object.keys(obj).reverse(); reversedKeys.forEach(key => { console.log(key, obj[key]); // ๐๏ธ c three, b two, a one });
The first step is to get an array of the object's keys by using the Object.keys() method.
const obj = { a: 'one', b: 'two', c: 'three', }; const keys = Object.keys(obj); console.log(keys); // ๐๏ธ ['a', 'b','c']
Object.keys()
method returns the object's keys ordered in the same way as given by looping over the object's properties manually.We used the Array.reverse() method to reverse the array of keys.
The reverse()
method reverses the elements in the array in place and returns
the result.
const obj = { a: 'one', b: 'two', c: 'three', }; const keys = Object.keys(obj); console.log(keys); // ๐๏ธ ['a', 'b','c'] const reversed = keys.reverse(); console.log(reversed); // ๐๏ธ ['c', 'b', 'a']
reverse()
method changes the contents of the original array.const arr = ['a', 'b', 'c']; const reversed = arr.reverse(); console.log(reversed); // ๐๏ธ ['c', 'b', 'a'] console.log(arr); // ๐๏ธ ['c', 'b', 'a']
To circumvent mutating the original array, you will often have to create a copy
before calling the reverse()
method.
const arr = ['a', 'b', 'c']; const reversed = [...arr].reverse(); console.log(reversed); // ๐๏ธ ['c', 'b', 'a'] console.log(arr); // ๐๏ธ ['a', 'b', 'c']
The last step is to use the Array.forEach() method to iterate over the reversed array of keys.
We can then use each key to access the values in the object.
const obj = { a: 'one', b: 'two', c: 'three', }; const reversedKeys = Object.keys(obj).reverse(); reversedKeys.forEach((key, index) => { console.log(key, obj[key]); // ๐๏ธ c three, b two, a one console.log(index); // ๐๏ธ 0 1 2 });
You also have access to the index of the current iteration when using the
Array.forEach()
method.
If you only need to access the object's values, use the Object.values()
method
instead.
This is a three-step process:
Object.values()
method to get an array of the object's values.reverse()
method to reverse the array.forEach()
method to iterate over the array of values in reverse
order.const obj = { a: 'one', b: 'two', c: 'three', }; // ๐๏ธ [ 'three', 'two', 'one' ] const reversedValues = Object.values(obj).reverse(); reversedValues.forEach(value => { console.log(value); // ๐๏ธ three, two, one });
The Object.values() method returns an array of the object's values.
const obj = { a: 'one', b: 'two', c: 'three', }; // ๐๏ธ [ 'one', 'two', 'three' ] console.log(Object.values(obj)); // ๐๏ธ [ 'three', 'two', 'one' ] console.log(Object.values(obj).reverse());
We used the Array.reverse()
method to reverse the array and used the
forEach()
method to iterate over the reversed array.
The function we passed to the Array.forEach
method gets called with each
element in the array.
The forEach()
method returns undefined
, so we have to perform some kind of
mutation to persist the state.
const obj = { a: 1, b: 2, c: 3, }; // ๐๏ธ [3, 2, 1] const reversedValues = Object.values(obj).reverse(); let total = 0; reversedValues.forEach(value => { console.log(value); // ๐๏ธ 3, 2, 1 total += value; }); console.log(total); // ๐๏ธ 6
On each iteration, we add the current object value to the total
variable to
preserve the state.
You can also reverse the output of the Object.entries()
method to iterate over
an object in reverse order.
const obj = { a: 1, b: 2, c: 3, }; // ๐๏ธ [ [ 'c', 3 ], [ 'b', 2 ], [ 'a', 1 ] ] const reversedEntries = Object.entries(obj).reverse(); reversedEntries.forEach(([key, value]) => { console.log(key, value); // ๐๏ธ c 3, b 2, a 1 });
The Object.entries() method returns an array of the given object's key-value pairs.
const reversedEntries = Object.entries(obj).reverse(); // ๐๏ธ [ [ 'c', 3 ], [ 'b', 2 ], [ 'a', 1 ] ] console.log(reversedEntries);
On each iteration, we use destructuring assignment to assign the key and value to variables and log them to the console.
const [key, value] = ['a', 1]; console.log(key); // ๐๏ธ a console.log(value); // 1
The key
variable gets assigned the first array element and the value
variable - the second.
You can learn more about the related topics by checking out the following tutorials: