7

I want to convert an object with indexes as keys, into an array in JavaScript.

For example:

Input: obj ={1: 'banana', 2: 'apple',3:'orange' }

Output: ['orange','apple','banana' ]

I have tried with 'Array.prototype.reverse.apply(obj)', but not able to get the result.

 var obj ={1: 'banana', 2: 'apple',3:'orange' };
 var res =Array.prototype.reverse.apply(obj);
 console.log(res); // return the same object, not reverse

What are the other usages of Array.prototype.reverse()?

mikemaccana
  • 81,787
  • 73
  • 317
  • 396
Nilam
  • 105
  • 1
  • 1
  • 7
  • 17
    objects don't have any order. – Daniel A. White Apr 30 '16 at 18:52
  • You should be using an array if you want order – epascarello Apr 30 '16 at 18:52
  • Think of it this way if it helps: javascript objects are like blobs and arrays are like straight lines. You just told a blob to turn around 180 degrees – vtange Apr 30 '16 at 20:06
  • Thank You! I was trying to use Array.prototype.reverse.apply(obj). But now I got it. I can use it inside the function, and it will return reverse object values. – Nilam Apr 30 '16 at 22:42
  • 1
    I disagree with the closure. The order is given by the keys, which are array indices. – Oriol Apr 30 '16 at 22:51
  • 1
    @DanielA.White I can receive a JS object response where the keys are in a specific order which represent items on the page. It seems to me as though Js objects can definitely have an order. – ESR Jun 08 '18 at 03:52

4 Answers4

26

You can first convert your almost-array-like object to a real array, and then use .reverse():

Object.assign([], {1:'banana', 2:'apple', 3:'orange'}).reverse();
// [ "orange", "apple", "banana", <1 empty slot> ]

The empty slot at the end if cause because your first index is 1 instead of 0. You can remove the empty slot with .length-- or .pop().

Alternatively, if you want to borrow .reverse and call it on the same object, it must be a fully-array-like object. That is, it needs a length property:

Array.prototype.reverse.call({1:'banana', 2:'apple', 3:'orange', length:4});
// {0:"orange", 1:"apple", 3:"banana", length:4}

Note it will return the same fully-array-like object object, so it won't be a real array. You can then use delete to remove the length property.

Oriol
  • 225,583
  • 46
  • 371
  • 457
16

This is mine implementation of object rotation

    function reverseObject(object) {
        var newObject = {};
        var keys = [];

        for (var key in object) {
            keys.push(key);
        }

        for (var i = keys.length - 1; i >= 0; i--) {
          var value = object[keys[i]];
          newObject[keys[i]]= value;
        }       

        return newObject;
      }
Yossi Neiman
  • 785
  • 1
  • 6
  • 13
2

There is no point of doing this, cause object's properties do not have order. Properties order in objects is not guaranteed in JavaScript;

Since ECMAScript 2015, using the Map object could be an alternative. A Map shares some similarities with an Object and guarantees the keys order:

Konst
  • 4,023
  • 5
  • 22
  • 35
-2
var reverseObj = function(object) {
    var NewObj = {}, keysArr = Object.keys(object);
    for (var i = keysArr.length-1; i >= 0; i--) {
        NewObj[keysArr[i]] = object[keysArr[i]];
    }
    return NewObj;
}
xlm
  • 4,594
  • 13
  • 42
  • 47