如何重建这个javascript对象

时间:2014-09-17 13:23:28

标签: javascript arrays json object object-literal

我有一个对象类型如下:

{
"1": {
    "ref": "1",
    "active": "1",
    "showorder": "1",
    "title": "Test 1"
},
"2": {
    "ref": "2",
    "active": "1",
    "showorder": "2",
    "title": "Test 2"
},
"3": {
    "ref": "3",
    "active": "1",
    "showorder": "4",
    "title": "Test 4"
},
"4": {
    "ref": "4",
    "active": "1",
    "showorder": "9",
    "title": "Test 9"
},
"5": {
    "ref": "5",
    "active": "1",
    "showorder": "7",
    "title": "Test 7"
}
}

在showorder属性的基础上,我需要安排如下:

{
"1": {
    "ref": "1",
    "active": "1",
    "showorder": "1",
    "title": "Test 1"
},
"2": {
    "ref": "2",
    "active": "1",
    "showorder": "2",
    "title": "Test 2"
},
"3": {
    "ref": "3",
    "active": "1",
    "showorder": "3",
    "title": "Test 3"
},
"4": {
    "ref": "4",
    "active": "1",
    "showorder": "4",
    "title": "Test 4"
},
"5": {
    "ref": "5",
    "active": "1",
    "showorder": "5",
    "title": "Test 5"
}
}

这是我失败的尝试,以为我会将对象传递给此函数:

function reArrangeTilesObject(tilesObj){
var newObj = [];
for(var i in tilesObj){
    var j = 1;
    if(j == tilesObj[i].showorder){
        newObj.push(tilesObj[i]);
        j++;
    }           
}
return newObj;
}

对不起,如果我不清楚。该对象以随机方式创建。所以我想要的是按照showorder属性排列的对象。所以如果showorder是4它应该在4的键中,并且没有showorder 9的对象.showorder 9应该在9的键下。谢谢

5 个答案:

答案 0 :(得分:0)

我可能误解了一些事情,但是:

for (var i = 0; i < tilesObj; i++)
{
    tilesObj[i].showOrder = i;
    tilesObj[i].title = "Test "+i;
}

for (var i in tilesObj)
{
    tilesObj[i].showOrder = tilesObj[i].ref;
    tilesObj[i].title = "Test "+tilesObj[i].ref;
}

我会相信这个例子。

答案 1 :(得分:0)

步骤:

  1. 将对象转换为数组(对象无序,数组已订购)
  2. Sort数组
  3. 代码:

    function reArrangeTilesObject(tilesObj) {
        var tilesArray = [];
    
        // Step 1 - convert to array
    
        for (var i in tilesObj) {
            tilesArray.push(tilesObj[i]);
        }
    
        // Step 2 - sort
    
        tilesArray.sort(function(a, b) {
            return a.showorder - b.showorder;
        });
    
        return tilesArray;
    }
    

    结果 - 有序数组:

    [
        {"ref":"1","active":"1","showorder":"1","title":"Test 1"},
        {"ref":"2","active":"1","showorder":"2","title":"Test 2"},
        {"ref":"3","active":"1","showorder":"4","title":"Test 4"},
        {"ref":"5","active":"1","showorder":"7","title":"Test 7"},
        {"ref":"4","active":"1","showorder":"9","title":"Test 9"}
    ]
    

答案 2 :(得分:0)

如果您尝试重命名对象数组中的属性,或者将它们排序为新数组,我无法理解。

如果您正在尝试排序:

function reArrangeTilesObject(tilesObj) {       
    var newObj = [];        
    for (var i = 1; i <= tilesObj.length; i++) {
        for (var j in tilesObj) {
            if (i == tilesObj[j].showorder) {
                newObj.push(tilesObj[j]);
                break;
            }
        }
    }
    return newObj;
}

应根据其showorder属性将它们添加到newObj数组中。

答案 3 :(得分:0)

如果您只是重写showordertitle值,这将起作用

function fix(o) {
    for(var n in o) {
        o[n].showorder = n;
        o[n].title = "Test " + n;
    }
}

答案 4 :(得分:0)

试试这个(demo):

function reArrangeTilesObject(tilesObj){
    var result = {};
    for(var key in tilesObj){
        content = result[key] = tilesObj[key];
        content.showorder = key;
        content.title = "Test " + key;
    }
    return result;
}

它将以原始格式(字典)返回一个新对象。 如果要返回一个数组(并处理可能的空索引),只需将var result = {};更改为var result = [];