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?
-
Why? For iterating? For serializing?Richard JP Le Guen– Richard JP Le Guen2012年11月29日 03:58:59 +00:00Commented Nov 29, 2012 at 3:58
-
You should use an array of javascript objects refer this question here stackoverflow.com/questions/979256/…Switch– Switch2012年11月29日 04:39:55 +00:00Commented Nov 29, 2012 at 4:39
5 Answers 5
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.
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
}
]
}
-
"ivrItems" : [ "50b5e7bec90a6f4e19000001" : { "name" : "sdf", "key" : "555", "onSelect" : "fsdfsdfsdf" }, "50b5e7c3c90a6f4e19000002" : { "name" : "dfgdf", "key" : "666", "onSelect" : "fdgdfgdf", "parentId" : null }] some thing like thisgauti– gauti2012年11月29日 04:05:01 +00:00Commented Nov 29, 2012 at 4:05
-
@GowtGM: No; that's invalid syntax.SLaks– SLaks2012年11月29日 04:17:41 +00:00Commented Nov 29, 2012 at 4:17
-
@GowtGM: I add sample in the answer.Chris Li– Chris Li2012年11月29日 05:55:55 +00:00Commented Nov 29, 2012 at 5:55
-
There is an answer that did the job for me here : 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– Benj2017年08月25日 14:43:14 +00:00Commented Aug 25, 2017 at 14:43 -
1This is wrong. JavaScript objects are ordered.Glenn Maynard– Glenn Maynard2022年06月15日 06:43:39 +00:00Commented Jun 15, 2022 at 6:43
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.
Javascript objects are intrinsically unordered.
You can't do that.
-
@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– gauti2012年11月29日 04:19:48 +00:00Commented Nov 29, 2012 at 4:19
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));