如何对 Javascript 对象进行排序,或将其转换为数组? [英] How to sort a Javascript object, or convert it to an array?

查看:22
本文介绍了如何对 Javascript 对象进行排序,或将其转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些从服务器获取的 JSON 数据.在我的 JavaScript 中,我想对其进行一些排序.我认为 sort() 函数会做我想要的.

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 数据到达时立即将其转换为对象.如果我尝试使用 sort() 方法,我会得到很多错误(使用 Firebug 进行测试).

However, it seems that JavaScript is converting the JSON data into an Object immediately on arrival. If I try to use the sort() method, I get errors a-plenty (using Firebug for testing).

我在网上找了一圈,似乎每个人都说,一方面,JSON 对象已经是 JavaScript 数组,而且对象可以像数组一样对待.喜欢在这个问题上,其中一个答案,有人说[Object 对象] 是你的数据——你可以像访问数组一样访问它."

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 将其视为一个或类似的内容.

However, that is not exactly true. JavaScript won't let me use sort() on my object. And since the default assumption is that they're all the same thing, there don't seem to be any instructions anywhere on how to convert an Object to an Array, or force JavaScript to treat it as one, or anything like that.

那么……我如何让 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"):

对象 JSON 数据

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

推荐答案

Array.prototype.slice.call(arrayLikeObject)

是将类数组对象转换为数组的标准方法.

is the standard way to convert and an array-like object to an array.

这仅适用于 arguments 对象.将通用对象转换为数组有点麻烦.以下是 underscore.js 的来源:

That only really works for the arguments object. To convert a generic object to an array is a bit of a pain. Here's the source from underscore.js:

_.toArray = function(iterable) {
    if (!iterable)                return [];
    if (iterable.toArray)         return iterable.toArray();
    if (_.isArray(iterable))      return iterable;
    if (_.isArguments(iterable))  return slice.call(iterable);
    return _.values(iterable);
};

_.values = function(obj) {
    return _.map(obj, _.identity);
};

事实证明,您将需要遍历您的对象并自己将其映射到数组.

Turns out you're going to need to loop over your object and map it to an array yourself.

var newArray = []
for (var key in object) {
    newArray.push(key);
}

您混淆了数组和关联数组"的概念.在 JavaScript 中,对象有点像关联数组,因为您可以以 object["key"] 格式访问数据.它们不是真正的关联数组,因为对象是无序列表.

You're confusing the concepts of arrays and "associative arrays". In JavaScript, objects kind of act like an associative array since you can access data in the format object["key"]. They're not real associative arrays since objects are unordered lists.

对象和数组有很大的不同.

Objects and arrays are vastly different.

使用下划线的示例:

var sortedObject = _.sortBy(object, function(val, key, object) {
    // return an number to index it by. then it is sorted from smallest to largest number
    return val;
});

参见现场示例

这篇关于如何对 Javascript 对象进行排序,或将其转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆