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
19.7k8 gold badges61 silver badges85 bronze badges
asked Nov 29, 2012 at 3:50
2
  • Why? For iterating? For serializing? Commented Nov 29, 2012 at 3:58
  • You should use an array of javascript objects refer this question here stackoverflow.com/questions/979256/… Commented Nov 29, 2012 at 4:39

5 Answers 5

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.

answered Nov 29, 2012 at 3:55
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
 }
 ]
}
answered Nov 29, 2012 at 3:54
5
  • "ivrItems" : [ "50b5e7bec90a6f4e19000001" : { "name" : "sdf", "key" : "555", "onSelect" : "fsdfsdfsdf" }, "50b5e7c3c90a6f4e19000002" : { "name" : "dfgdf", "key" : "666", "onSelect" : "fdgdfgdf", "parentId" : null }] some thing like this Commented Nov 29, 2012 at 4:05
  • @GowtGM: No; that's invalid syntax. Commented Nov 29, 2012 at 4:17
  • @GowtGM: I add sample in the answer. Commented 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. Commented Aug 25, 2017 at 14:43
  • 1
    This is wrong. JavaScript objects are ordered. Commented Jun 15, 2022 at 6: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.

answered Nov 29, 2012 at 3:57
0

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

answered Nov 29, 2012 at 3:51
1
  • @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... Commented Nov 29, 2012 at 4:19
0

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));
answered Mar 24, 2024 at 17:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.