Javascript - 将大对象文字转换为另一个对象文字

时间:2013-03-09 00:01:35

标签: javascript jquery json object loops

我正在尝试将Javascript对象文字转换为另一个。我认为有可能有一些循环,但我无法完成它。目标结构如下所示,“convertedData”。

可以在这里找到小提琴:http://jsbin.com/ajemih/9/edit

这是JSON数据:

var data =

{
"29-10-2012": {
    "1a": {
            "allMovement": "1",
            "allLoad": "2",
            "loadMovement": "3"
    },
        "1b": {
            "allMovement": 4,
            "allLoad": 5,
            "loadMovement": 6
    }
},
    "22-02-2013": {
    "1a": {
            "allMovement": "7",
            "allLoad": "8",
            "loadMovement": "9"
    },
        "1b": {
            "allMovement": "10",
            "allLoad": "11",
            "loadMovement": "12"
    }
}
};

for (day in data) {

    for (id in data[day]) {

        document.write(data[day][id].allMovement+"<br>");
        document.write(data[day][id].allLoad+"<br>");
        document.write(data[day][id].loadMovement+"<br>");

    }
}

/*

convertedData = [[1,7],
             [2, 8],
             [3, 9],
             ["4","10"],
             ["5","11"],
             ["6", "12"]];


convertedData = [["1a-allMovement-29-10-2012","1a-allMovement-22-02-2013],
                 ["1a-allLoad-29-10-2012", "1a-allLoad22-02-2013"],
                 ["1a-loadMovement-29-10-2012", "1a-loadMovement-22-02-2013"],
                 ["1b-allMovement-29-10-2012","1a-allMovement-22-02-2013"],
                 ["1b-allLoad-29-10-2012","1b-allLoad22-02-2013"],
                 ["1b-loadMovement-29-10-2012", "1b-loadMovement-22-02-2013"]];

*/

4 个答案:

答案 0 :(得分:4)

尝试这样的事情,根据需要进行调整:

var out = [],
    dateKey, idx, item, itemKey, inner;

for (dateKey in data) {
    if (data.hasOwnProperty(dateKey)) {
        idx = out.length;
        out[idx] = [];
        item = data[dateKey];
        for (itemKey in item) {
            if (item.hasOwnProperty(itemKey)) {
                inner = item[itemKey];
                out[idx].push(itemKey + '-' + inner.allMovement + '-' + inner.allLoad + '-' + inner.loadMovement);
            }
        } 
    }
}
console.log(out);

答案 1 :(得分:2)

这是一个使用jQuery.each()函数的解决方案:

var convertedObj = {};
$.each(data, function(i0,val0) {
    $.each(val0, function(i1,val1) {
        $.each(val1, function(i2,val2) {
            if (!convertedObj[i1+"-"+i2]) convertedObj[i1+"-"+i2] = [];
            convertedObj[i1+"-"+i2].push(val2);            
        });
    });
});
var convertedData = [];
$.each(convertedObj, function(i,val) {
    convertedData.push(val);
});

这是link to a jsbin fiddle

(参见控制台中的结果)

答案 2 :(得分:2)

使用underscore.js:

var memo = {};

_.each(data, function (elem) { 
  _.each(elem, function (elem2, idx) {
    _.each (elem2, function(elem3, idx2) {
      if (typeof memo[idx + idx2] === 'undefined')
          memo[idx + idx2] = [];
      memo[idx + idx2].push(elem3);
    });
  });
});

memo = _.map(memo, function(e) { return e; });

// printing
document.write('[');
_.each(memo, function (e, idx) {
  document.write("[");
  _.each(e, function(ie, iidx) {
    if (typeof ie === 'string')
      document.write('"' + ie + '"');
    else
      document.write(ie);

    if (iidx != e.length - 1)
        document.write(",");
  });
  document.write("]");
  if (idx != memo.length - 1)
    document.write(",");
});
document.write(']');

输出

[["1","7"],["2","8"],["3","9"],[4,"10"],[5,"11"],[6,"12"]]

jsbin link - &gt; http://jsbin.com/ajemih/11

我认为你有混乱的输入数据,因为它不符合类型的输出。除非它也是这个练习的一部分......: - )

PS。据我所知你想要文字,这就是为什么有这整个document.write的部分,但你可以跳过它,如果你不想打印它

MZ

答案 3 :(得分:2)

DEMO: http://jsfiddle.net/XvwkX/1/

我已经完成了2次传递,第1次传递是构造一个简单的结果,它解析原始数据对象并创建数字作为键的字符串文字,第二遍用于更新convertedData var相应的值。

var easyResult = {};
//first pass
$.each(data, function (d, o1) { //date, object1
    $.each (o1, function (t, o2) { //type, object2
        $.each(o2, function (s, n) { //string, ID
            easyResult[n] = t + '-' + s + '-' + d;
        });
    });
});

for (var i = 0; i < convertedData.length; i++ ) {
    for (var j = 0; j < convertedData[i].length; j++) {
        convertedData[i][j] = easyResult[""+convertedData[i][j]];
    }
}
相关问题