如何使用javascript对json数据进行排序

时间:2017-03-19 04:39:22

标签: javascript arrays json

我的$ http响应以下列格式给出了结果:

$scope.rooms = {  
    '2B' : [    
        {"RoomEmailId":"2B-323@me.com","RoomName":"2B-323"},    
        {"RoomEmailId":"2B-123@me.com","RoomName":"2B-123"}     
    ],   
    '5A' : [  
        {"RoomEmailId":"5A-323@me.com","RoomName":"5A-323"},   
        {"RoomEmailId":"5A-123@me.com","RoomName":"5A-123"},  
        {"RoomEmailId":"5A-423@me.com","RoomName":"5A-423"}  
    ],  
    '1A' : [  
        {"RoomEmailId":"1A-323@me.com","RoomName":"1A-323"},  
        {"RoomEmailId":"1A-123@me.com","RoomName":"1A-123"},  
        {"RoomEmailId":"1A-423@me.com","RoomName":"1A-423"}  
    ]  
}

我想按以下顺序对此结果进行排序:

$scope.rooms = {  
    '1A' : [  
        {"RoomEmailId":"1A-123@me.com","RoomName":"1A-123"},  
        {"RoomEmailId":"1A-323@me.com","RoomName":"1A-323"},  
        {"RoomEmailId":"1A-423@me.com","RoomName":"1A-423"}  
    ]  
    '2B' : [  
        {"RoomEmailId":"2B-123@me.com","RoomName":"2B-123"},  
        {"RoomEmailId":"2B-323@me.com","RoomName":"2B-323"}       
    ],  
    '5A' : [  
        {"RoomEmailId":"5A-123@me.com","RoomName":"5A-123"},  
        {"RoomEmailId":"5A-323@me.com","RoomName":"5A-323"},        
        {"RoomEmailId":"5A-423@me.com","RoomName":"5A-423"}              
    ],  
}

如何使用javascript对其进行排序?

2 个答案:

答案 0 :(得分:0)

你想要的是这样的吗?delete并将值重新分配给对象。版权所有:sort(rooms,by("RoomName","RoomEmailId"))

代码

function by(columns) {
    columns = typeof columns == "string" ? columns.split(",") : columns;

    function compare(a, b) {
        return a > b ? 1 : a < b ? -1 : 0;
    }

    return function (a, b) {
        for (var i in columns) {
            var p = columns[i];
            var it = compare(a[p], b[p]);
            if (it) {
                return it;
            }
        }
        return 0;
    }
}

function sort(o, comparator) {
    return Object.keys(o).sort().reduce(function (o, key) {
        var it = o[key];
        delete o[key];
        o[key] = it.sort ? it.sort(comparator) : it;
        return o;
    }, o);
}

测试

test("sort keys", () => {
    let o = {b: 2, a: 1};

    expect(Object.keys(o)).toEqual(["b", "a"]);
    expect(sort(o, null)).toEqual({a: 1, b: 2});
    expect(Object.keys(o)).toEqual(["a", "b"]);
});

test("sort values", () => {
    let o = {b: [{value: 2}, {value: 1}], a: [{value: 4}, {value: 3}]};

    expect(Object.keys(o)).toEqual(["b", "a"]);
    expect(sort(o, by("value"))).toEqual({b: [{value: 1}, {value: 2}], a: [{value: 3}, {value: 4}]});
    expect(Object.keys(o)).toEqual(["a", "b"]);
});

演示

var rooms = {  
    '2B' : [    
        {"RoomEmailId":"2B-323@me.com","RoomName":"2B-323"},    
        {"RoomEmailId":"2B-123@me.com","RoomName":"2B-123"}     
    ],   
    '5A' : [  
        {"RoomEmailId":"5A-323@me.com","RoomName":"5A-323"},   
        {"RoomEmailId":"5A-123@me.com","RoomName":"5A-123"},  
        {"RoomEmailId":"5A-423@me.com","RoomName":"5A-423"}  
    ],  
    '1A' : [  
        {"RoomEmailId":"1A-323@me.com","RoomName":"1A-323"},  
        {"RoomEmailId":"1A-123@me.com","RoomName":"1A-123"},  
        {"RoomEmailId":"1A-423@me.com","RoomName":"1A-423"}  
    ]  
};
function by(columns) {
    columns = typeof columns == "string" ? columns.split(",") : columns;

    function compare(a, b) {
        return a > b ? 1 : a < b ? -1 : 0;
    }

    return function (a, b) {
        for (var i in columns) {
            var p = columns[i];
            var it = compare(a[p], b[p]);
            if (it) {
                return it;
            }
        }
        return 0;
    }
}

function sort(o, comparator) {
    return Object.keys(o).sort().reduce(function (o, key) {
        var it = o[key];
        delete o[key];
        o[key] = it.sort ? it.sort(comparator) : it;
        return o;
    }, o);
}
console.log("unsorted rooms:",rooms);
console.log("sorted rooms:",sort(rooms,by("RoomName","RoomEmailId")));

答案 1 :(得分:0)

我可以看到你在对象内移动了键。对象键值不可排序,您需要将它们放在一个数组中才能实现。如果您只想对作为这些键的值的数组进行排序,那么您可以reduce对象键并根据需要返回一个新对象(此解决方案很好,因为您不会改变原始的{{ 1}}对象)

rooms

这里有效: http://jsbin.com/paqavaxako/edit?html,js,console