How to reverse Values sequence in an Object?

   JavaScript

The JavaScript array reverse() method changes element’s sequence of an array and returns the reverse sequence.  We will use that approach to reverse values sequence in the given object.

Solution

var obj = {"a": "1", "b": "2", "c": "3", "d": "4", "e": "5"};

// Create an empty array
var arr = [];

// Run through a loop
for (prop in obj) {
  arr.push(obj[prop]);
}

// Reverse the array items using Array.reverse() method
arr.reverse();

// Final output
["5", "4", "3", "2", "1"]
Tags: Array reverse methodJavaScript

Leave a Comment