I receive from the server a JSON like the following:
我从服务器收到一个JSON,如下所示:
{"sent":{"Executive":1},"received":{"Executive":1},"viewed":{"Executive":1}}
how to convert this structure to a JavaScript multidimensional array?
如何将此结构转换为JavaScript多维数组?
I would want something like this:
我想要这样的东西:
var sent = [["Executive", 1]];
var received = [["Executive", 1]];
var viewed = [["Executive", 1]];
2 个解决方案
#1
1
The simplest and most dynamic way surely is a good old for
-loop. Javascript doesn't ship with built-ins making this task easier.
最简单,最动态的方式肯定是一个很好的旧循环。 Javascript不附带内置插件,使此任务更容易。
An example of a 1d-array. I leave it open to the thread creator to add another, inner for-loop.
1d阵列的示例。我让它对线程创建者开放,以添加另一个内部for循环。
var json_data = {"sent": 1, "received": 2};
var result = [];
for(var i in json_data)
result.push([i, json_data [i]]);
#2
0
var plotdata = [];
var scale = [];
var color = new Array();
color[0] = "#3983C2";
color[1] = "#F79263";
color[2] = "#CB6FD7";
color[3] = "#87B87F";
color[4] = "#9ABC32";
color[5] = "#D53F40";
color[6] = "#999999";
var x = 0;
for(var i in json_data) {
var obj = json_data[i];
var d1 = [];
for(var k in obj) {
d1.push([k, obj [k]]);
scale.push([obj [k]]);
}
x++;
plotdata.push({ color: color[x], label: i, data: d1});
}
#1
1
The simplest and most dynamic way surely is a good old for
-loop. Javascript doesn't ship with built-ins making this task easier.
最简单,最动态的方式肯定是一个很好的旧循环。 Javascript不附带内置插件,使此任务更容易。
An example of a 1d-array. I leave it open to the thread creator to add another, inner for-loop.
1d阵列的示例。我让它对线程创建者开放,以添加另一个内部for循环。
var json_data = {"sent": 1, "received": 2};
var result = [];
for(var i in json_data)
result.push([i, json_data [i]]);
#2
0
var plotdata = [];
var scale = [];
var color = new Array();
color[0] = "#3983C2";
color[1] = "#F79263";
color[2] = "#CB6FD7";
color[3] = "#87B87F";
color[4] = "#9ABC32";
color[5] = "#D53F40";
color[6] = "#999999";
var x = 0;
for(var i in json_data) {
var obj = json_data[i];
var d1 = [];
for(var k in obj) {
d1.push([k, obj [k]]);
scale.push([obj [k]]);
}
x++;
plotdata.push({ color: color[x], label: i, data: d1});
}