8

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()?

6
  • 17
    objects don't have any order. Commented Apr 30, 2016 at 18:52
  • You should be using an array if you want order Commented Apr 30, 2016 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 Commented Apr 30, 2016 at 20:06
  • 1
    I disagree with the closure. The order is given by the keys, which are array indices. Commented Apr 30, 2016 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. Commented Jun 8, 2018 at 3:52

4 Answers 4

29

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.

Sign up to request clarification or add additional context in comments.

Comments

17

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;
      }

2 Comments

nice job , simple for understand.
I believe the for...in in for (var key in object) { doesn't guarantee order, so you might not get perfect rotation.
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:

1 Comment

Objetcs do have an order, which is set by the order in which elements were defined
-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;
}

2 Comments

Add some comments to your code
Not working....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.