gauti
gauti

Reputation: 1274

How to change the order of a JavaScript object?

My JavaScript object looks like this:

"ivrItems": {
  "50b5e7bec90a6f4e19000001": {
    "name": "sdf",
    "key": "555",
    "onSelect": "fsdfsdfsdf"
  },
  "50b5e7c3c90a6f4e19000002": {
    "name": "dfgdf",
    "key": "666",
    "onSelect": "fdgdfgdf",
    "parentId": null
  },
  "50b5e7c8c90a6f4e19000003": {
    "name": "dfdf",
    "key": "55",
    "onSelect": "dfdffffffffff",
    "parentId": null
  }
}

Now I want to change the order of the object dynamically.

After sorting, the object should look as follows:

"ivrItems": {
  "50b5e7bec90a6f4e19000001": {
    "name": "sdf",
    "key": "555",
    "onSelect": "fsdfsdfsdf"
  },
  "50b5e7c8c90a6f4e19000003": {
    "name": "dfdf",
    "key": "55",
    "onSelect": "dfdffffffffff",
    "parentId": null
  }
  "50b5e7c3c90a6f4e19000002": {
    "name": "dfgdf",
    "key": "666",
    "onSelect": "fdgdfgdf",
    "parentId": null
  }
}

Is there any possible way to do this?

Upvotes: 11

Views: 14841

Answers (5)

cdauth
cdauth

Reputation: 7558

While this is not recommendable, as by the JavaScript standard the order of object properties is not defined, in practice the properties are ordered chronologically by their time of addition. You can create a sorted copy of your object likes this:

const sorted = Object.fromEntries(Object.entries(object).sort((a, b) => a[1].key - b[1].key));

Upvotes: 0

Chris Li
Chris Li

Reputation: 3725

You should use an Array. Object keys has no order

like this:

{
    "ivrItems": [
        {
            "id": "50b5e7bec90a6f4e19000001",
            "name": "sdf",
            "key": "555",
            "onSelect": "fsdfsdfsdf"
        },
        {
            "id": "50b5e7c8c90a6f4e19000003",
            "name": "dfdf",
            "key": "55",
            "onSelect": "dfdffffffffff",
            "parentId": null
        },
        {
            "id": "50b5e7c3c90a6f4e19000002",
            "name": "dfgdf",
            "key": "666",
            "onSelect": "fdgdfgdf",
            "parentId": null
        }
    ]
}

Upvotes: 2

I Hate Lazy
I Hate Lazy

Reputation: 48761

To get and then change the order of an Object's enumeration, you need to manually define the order. This is normally done by adding the properties of the object to an Array.

var keys = Object.keys(data.ivrItems);

Now you can iterate the keys Array, and use the keys to access members of your irvItems object.

keys.forEach(function(key) {
    console.log(data.irvItems[key]);
});

Now the order will always be that of the order given by Object.keys, but there's no guarantee that the order will be what you want.

You can take that Array and reorder it using whatever ordering you need.

keys.sort(function(a, b) {
    return +data.irvItems[a].key - +data.irvItems[b].key;
});

This sort will sort the keys by the nested key property of each object after numeric conversion.

Upvotes: 4

chbrown
chbrown

Reputation: 12028

You're probably going to have a tough time with cross-browser compatibility, if you're doing this in the browser. But computers are mostly deterministic, so you could probably accomplish this reliably in one javascript engine implementation, though. For example, in the Chrome REPL / console, you can get this order simply by sequencing adding the properties:

var n = {}
n.b = 2
n.c = 3
var m = {}
m.c = 3
m.b = 2
JSON.stringify(n)
> "{"b":2,"c":3}"
JSON.stringify(m)
> "{"c":3,"b":2}"

So you could reconstruct your object, adding the keys in the order you want to find them later.

But the other people are right, if you want true, predictable order, you should use an array.

Upvotes: 2

SLaks
SLaks

Reputation: 887453

Javascript objects are intrinsically unordered.
You can't do that.

Upvotes: 0

Related Questions