I have some JSON data that I get from a server. In my JavaScript, I want to do some sorting on it. I think the sort() function will do what I want.
但是,JavaScript似乎是在JSON数据到达时立即将其转换为对象.如果我try 使用ort()方法,我会收到大量错误(使用Firebug进行测试).
I've looked around the net, and everyone seems to say that for one thing, JSON objects are already JavaScript arrays, and also that Objects can be treated just like arrays. Like over on this question, where in one of the answers, a guy says "The [Object object] is your data -- you can access it as you would an array."
然而,事实并非如此.JavaScript不允许我在对象上使用sort().由于默认的假设是它们都是一样的,所以似乎没有任何关于如何将对象转换为数组,或强制JavaScript将其视为一个数组,或诸如此类的指令.
So... how do I get JavaScript to let me treat this data as an array and sort() it?
Console log output of my object looks like this (I want to be able to sort by the values in the "level"):
OBJECT JSONdata
{
1: {
displayName: "Dude1",
email: "dude1@example.com<mailto:dude1@example.com>",
lastActive: 1296980700,
level: 57,
timeout: 12969932837
}, 2: {
displayName: "Dude2",
email: "dude2@example.com<mailto:dude2@example.com>",
lastActive: 1296983456,
level: 28,
timeout: 12969937382
}, 3: {
displayName: "Dude3",
email: "dude3@example.com<mailto:dude3@example.com>",
lastActive: 1296980749,
level: 99,
timeout: 129699323459
}
}