来自JSON对象的多维数组

时间:2012-08-22 11:46:23

标签: javascript json

来自下面的JSON对象

{cols:[{"id":"t","label":"Title","type":"string"},{"id":"l","label":"Avg ","type":"string"},{"id":"lb","label":"High","type":"string"},{"id":"lo","label":"Low","type":"string"}],rows:[{"c":[{"v":"Change navigation"},{"v":5.6666666666667},{"v":"10"},{"v":"1"}]},{"c":[{"v":"Executive leadership"},{"v":6.0666666666667},{"v":"7"},{"v":"3"}]},{"c":[{"v":"Business ownership"},{"v":5.8095238095238},{"v":"10"},{"v":"2"}]},{"c":[{"v":"Change enablement"},{"v":6.4285714285714},{"v":"9"},{"v":"5"}]}]}

我怎么能得到像

这样的东西
[['Change navigation',6.5333333333333],['Executive leadership',6.0666666666667],['Business ownership',5.8095238095238],['Change enablement',6.4285714285714]]

有人发布了来自this.cant的一维数组的代码,在javascript中找出了多维数据

var arr = [],
i = 0;
for (; i < json.rows.length; i++) {
arr.push(json.rows[i].c[0].v + '=' + json.rows[i].c[1].v);

}

给出了

['Change navigation=6.5333333333333','Executive leadership=6.0666666666667', 'Business ownership=5.8095238095238','Change enablement=6.4285714285714']

1 个答案:

答案 0 :(得分:3)

不是推送连接字符串,而是将整个数组推送到现有数组:

var arr = [];
for(var i = 0, l = json.rows.length; i < l; i++) {
    arr.push([ json.rows[i].c[0].v, json.rows[i].c[1].v ]);
}