11

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?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
gauti
  • 1,264
  • 3
  • 17
  • 43

4 Answers4

4

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.

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
2

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
        }
    ]
}
Chris Li
  • 3,715
  • 3
  • 29
  • 31
  • "ivrItems" : [ "50b5e7bec90a6f4e19000001" : { "name" : "sdf", "key" : "555", "onSelect" : "fsdfsdfsdf" }, "50b5e7c3c90a6f4e19000002" : { "name" : "dfgdf", "key" : "666", "onSelect" : "fdgdfgdf", "parentId" : null }] some thing like this – gauti Nov 29 '12 at 04:05
  • @GowtGM: No; that's invalid syntax. – SLaks Nov 29 '12 at 04:17
  • @GowtGM: I add sample in the answer. – Chris Li Nov 29 '12 at 05:55
  • There is an answer that did the job for me here : https://stackoverflow.com/a/16543302/1579667 which says to use the second parameter of `JSON.stringify()` with an array of key names in the order you want them. – Benj Aug 25 '17 at 14:43
  • 1
    This is wrong. JavaScript objects *are* ordered. – Glenn Maynard Jun 15 '22 at 06:43
2

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.

chbrown
  • 11,865
  • 2
  • 52
  • 60
0

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • @Thanks , Any idea to change the above object to array, the keys 50b5e7bec90a6f4e19000001,.... are very important, At any given point i know only this keys... – gauti Nov 29 '12 at 04:19