动态内置的JavaScript

时间:2018-01-31 11:07:13

标签: javascript jquery

我需要根据对象的长度生成“数据集”,如果它包含3个项目,那么我的“数据集”应该创建3个项目。在下面的例子中,r.d.BarData.length = 2,所以我将在“数据集”中有两个列表。 BarData中的项目可能会有所不同,因此应创建“数据集”

//r.d.BarData.length = 2
//Then it should generate 
 datasets: [
    {
        label: "My First dataset",
        fillColor: "rgba(220,220,220,0.5)",
        strokeColor: "rgba(220,220,220,0.8)",
        highlightFill: "rgba(220,220,220,0.75)",
        highlightStroke: "rgba(220,220,220,1)",
        data: r.d.BarData[0].split(",")
    },
    {
        label: "My Second dataset",
        fillColor: "rgba(151,187,205,0.5)",
        strokeColor: "rgba(151,187,205,0.8)",
        highlightFill: "rgba(151,187,205,0.75)",
        highlightStroke: "rgba(151,187,205,1)",
        data: r.d.BarData[1].split(",")
    }
]

1 个答案:

答案 0 :(得分:1)

基本上你可以使用循环来生成数据集

 datasets = []
  $.each(r.d.BarData, function(i, d){
    datasets.push({
      label: "My First dataset",
      fillColor: "rgba(220,220,220,0.5)",
      strokeColor: "rgba(220,220,220,0.8)",
      highlightFill: "rgba(220,220,220,0.75)",
      highlightStroke: "rgba(220,220,220,1)",
      data: d.split(",")
    });
  });