最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to change the order of a JavaScript object? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Nov 24, 2017 at 18:58 Sebastian Simon 19.5k8 gold badges60 silver badges84 bronze badges asked Nov 29, 2012 at 3:50 gautigauti 1,2744 gold badges18 silver badges43 bronze badges 2
  • Why? For iterating? For serializing? – Richard JP Le Guen Commented Nov 29, 2012 at 3:58
  • You should use an array of javascript objects refer this question here stackoverflow.com/questions/979256/… – Switch Commented Nov 29, 2012 at 4:39
Add a comment  | 

5 Answers 5

Reset to default 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.

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

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.

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));
发布评论

评论列表(0)

  1. 暂无评论