简体   繁体   English

在javascript中排序json对象

[英]sort json object in javascript

For example with have this code: 例如,有这个代码:

var json = {
    "user1" : {
        "id" : 3
    },
    "user2" : {
        "id" : 6
    },
    "user3" : {
        "id" : 1
    }
}

How can I sort this json to be like this - 我怎么能把这个json排成这样 -

var json = {
    "user3" : {
        "id" : 1
    },
    "user1" : {
        "id" : 3
    },
    "user2" : {
        "id" : 6
    }
}

I sorted the users with the IDs.. 我用ID对用户进行了排序..
I don't know how to do this in javascript.. 我不知道如何在javascript中执行此操作..

First off, that's not JSON. 首先,那不是 JSON。 It's a JavaScript object literal. 它是一个JavaScript对象文字。 JSON is a string representation of data, that just so happens to very closely resemble JavaScript syntax. JSON是数据的字符串表示 ,恰好与JavaScript语法非常相似。

Second, you have an object. 其次,你有一个对象。 They are unsorted. 他们没有分类。 The order of the elements cannot be guaranteed. 元素的顺序无法保证。 If you want guaranteed order, you need to use an array. 如果您需要保证订单,则需要使用数组。 This will require you to change your data structure. 这将要求您更改数据结构。

One option might be to make your data look like this: 一种选择可能是使您的数据看起来像这样:

var json = [{
    "name": "user1",
    "id": 3
}, {
    "name": "user2",
    "id": 6
}, {
    "name": "user3",
    "id": 1
}];

Now you have an array of objects, and we can sort it. 现在你有一个对象数组,我们可以对它进行排序。

json.sort(function(a, b){
    return a.id - b.id;
});

The resulting array will look like: 结果数组将如下所示:

[{
    "name": "user3",
    "id" : 1
}, {
    "name": "user1",
    "id" : 3
}, {
    "name": "user2",
    "id" : 6
}];

Here is a simple snippet that sorts a javascript representation of a Json. 这是一个简单的片段,用于对Json的javascript表示进行排序。

function isObject(v) {
    return '[object Object]' === Object.prototype.toString.call(v);
};

JSON.sort = function(o) {
if (Array.isArray(o)) {
        return o.sort().map(JSON.sort);
    } else if (isObject(o)) {
        return Object
            .keys(o)
        .sort()
            .reduce(function(a, k) {
                a[k] = JSON.sort(o[k]);

                return a;
            }, {});
    }

    return o;
}

It can be used as follows: 它可以使用如下:

JSON.sort({
    c: {
        c3: null,
        c1: undefined,
        c2: [3, 2, 1, 0],
    },
    a: 0,
    b: 'Fun'
});

That will output: 这将输出:

{
  a: 0,
  b: 'Fun',
  c: {
    c2: [3, 2, 1, 0],
    c3: null
  }
}

In some ways, your question seems very legitimate, but I still might label it an XY problem . 在某些方面,你的问题似乎非常合理,但我仍然可能将其标记为XY problem I'm guessing the end result is that you want to display the sorted values in some way? 我猜最终结果是你想以某种方式显示排序值? As Bergi said in the comments, you can never quite rely on Javascript objects ( {i_am: "an_object"} ) to show their properties in any particular order. 正如Bergi在评论中所说,你永远不能完全依赖 Javascript对象( {i_am: "an_object"} )以任何特定的顺序显示它们的属性。

For the displaying order, I might suggest you take each key of the object (ie, i_am ) and sort them into an ordered array. 对于显示顺序,我可能建议您获取对象的每个键(即i_am )并将它们排序为有序数组。 Then, use that array when retrieving elements of your object to display. 然后,在检索要显示的对象的元素时使用该数组。 Pseudocode: 伪代码:

var keys = [...]
var sortedKeys = [...]
for (var i = 0; i < sortedKeys.length; i++) {
  var key = sortedKeys[i];
  addObjectToTable(json[key]);
}
if(JSON.stringify(Object.keys(pcOrGroup).sort()) === JSON.stringify(Object.keys(orGroup)).sort())
{
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM