谷歌饼图的JSON格式

时间:2022-01-10 09:33:15

Hello i have a json file that has data stored like this:

您好我有一个json文件,其数据存储如下:

[{"cake": "cake1","order": 20},{"cake": "cake2","order": 34},{"cake": "cake3",
  "order": 11}, {"cake": "cake4","order": 11}, {"cake": "cake5", "order": 5}]

Now i want to represent the above data using google's pie chart but in order to do accomplish this, it needs to be in this format:

现在我想使用谷歌的饼图代表上述数据,但为了做到这一点,它需要采用以下格式:

[ ['Cake', 'No of Orders'], ['cake1', 20],['cake2',34],
  ['cake3', 11],['cake4',11],['cake5',5] ]

google's chart documentation specifies that data should be passed like this:

谷歌的图表文档指定数据应该像这样传递:

var data = google.visualization.arrayToDataTable([
          ['Cake', 'No of Orders'],
          ['cake1', 20],
          ['cake2', 34],
          ['cake3',  11],
          ['cake4', 11],
          ['cake5', 5]
        ]);

Please how does one change the original format above into a list containing severals list in javascript. Much help would be appreciated.

请问如何将上面的原始格式更改为包含javascript中几个列表的列表。很多帮助将不胜感激。

2 个解决方案

#1


1  

Perhaps using the array .map() method to change the format of the data, and then the array .unshift() method to insert the headings at the beginning of the new array:

也许使用数组.map()方法来改变数据的格式,然后使用数组.unshift()方法在新数组的开头插入标题:

var input = [{"cake": "cake1","order": 20},{"cake": "cake2","order": 34},
             {"cake": "cake3", "order": 11}, {"cake": "cake4","order": 11},
             {"cake": "cake5", "order": 5}];

var output = input.map(function(item) { return [item.cake, item.order]; });

output.unshift(["Cake", "No of Orders"]);

var data = google.visualization.arrayToDataTable(output);

#2


0  

How about something like this? Try running this in jsbin.

这样的事怎么样?尝试在jsbin中运行它。

var a = [{"cake": "cake1","order": 20},{"cake": "cake2","order": 34},{"cake": "cake3",
  "order": 11}, {"cake": "cake4","order": 11}, {"cake": "cake5", "order": 5}];

function cakeFormat(a){
  var first = [];
  console.log(a);
  // noprotect
  for(var i = 0; i < a.length; i++){
    for(var j in a[i]){
      first.push([j, a[i][j]]);
    }
  }

  var final = [["Cake", "No of Orders"]];

  for(var k = 0; k < (first.length - 1);){
    final.push([first[k][1], first[k+1][1]]);
    k = k + 2;
  }

  return final;
}

cakeFormat(a);

#1


1  

Perhaps using the array .map() method to change the format of the data, and then the array .unshift() method to insert the headings at the beginning of the new array:

也许使用数组.map()方法来改变数据的格式,然后使用数组.unshift()方法在新数组的开头插入标题:

var input = [{"cake": "cake1","order": 20},{"cake": "cake2","order": 34},
             {"cake": "cake3", "order": 11}, {"cake": "cake4","order": 11},
             {"cake": "cake5", "order": 5}];

var output = input.map(function(item) { return [item.cake, item.order]; });

output.unshift(["Cake", "No of Orders"]);

var data = google.visualization.arrayToDataTable(output);

#2


0  

How about something like this? Try running this in jsbin.

这样的事怎么样?尝试在jsbin中运行它。

var a = [{"cake": "cake1","order": 20},{"cake": "cake2","order": 34},{"cake": "cake3",
  "order": 11}, {"cake": "cake4","order": 11}, {"cake": "cake5", "order": 5}];

function cakeFormat(a){
  var first = [];
  console.log(a);
  // noprotect
  for(var i = 0; i < a.length; i++){
    for(var j in a[i]){
      first.push([j, a[i][j]]);
    }
  }

  var final = [["Cake", "No of Orders"]];

  for(var k = 0; k < (first.length - 1);){
    final.push([first[k][1], first[k+1][1]]);
    k = k + 2;
  }

  return final;
}

cakeFormat(a);