循环通过json数组属性

时间:2014-10-01 11:49:34

标签: javascript arrays json properties chart.js

我使用JQuery,ChartJS,Moment.js在同一页面上为多个图表收集JSON格式的数据,但来自同一个JSON源。在JSON中,高度对象是一个图形,长度是另一个图形。 这是JSON外观

示例
  "Series": {
    "heights": [
      {
        "Date": "2014-10-01",
        "Value": 22
      },
      {
        "Date": "2014-10-01",
        "Value": 53
      },
      {
        "Date": "2014-10-01",
        "Value": 57
      },
    ],
    "lengths": [
      {
        "Date": "2014-10-01",
        "Value": 54
      },
      {
        "Date": "2014-10-01",
        "Value": 33
      }
    ]
}

我设法循环显示JSON以显示每个图表,但我真的无法使用" DRY - 不要重复自己"办法。现在我有大量难以更新/读取的代码。

$.getJSON("data.json", function(data) {
                var dateArray = [];
                var valueArray = [];

                for ( var i = 0; i < data.Series["heights"].length; i++) {

                    var obj = data.Series.heights[i];
                    var date = obj["Date"].toString();
                    var Value = obj["Value"];

                    for ( var key in obj) {
                    //console.log(obj["Value"]);
                    //date = obj["Date"];
                    Value = obj[key].toString();

                }
                    valueArray.push(Value);
                    dateArray.push(moment(date).format("MMMM Mo"));

                var dataArray = {
                    labels: dateArray,
                    datasets: [
                    {
                        label: "Lengths",

                        strokeColor: "rgb(26, 188, 156)",
                        pointColor: "rgba(220,220,220,1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(220,220,220,1)",
                        data: valueArray
                    }
                    ]
                };

                }
                var ctx = document.getElementById("lengthsChart").getContext("2d");
                var myLineChart = new Chart(ctx).Line(dataArray, { 
                    scaleShowGridLines : true, 
                    bezierCurve : true, 
                    bezierCurveTension : 0.4, 
                    datasetStroke : false, 
                    fillColor: "rgba(0,0,0,0)", 
                    datasetFill : false,
                    responsive: true,
                    showTooltips: true,
                    animation: false
                });
});

现在我将这段代码放在一个开关语句中,用于&#34;高度&#34;,&#34;长度&#34;等...我猜这是一种可怕的方式。但我一直无法为各个图表制作循环。

我尝试过这样的事情:

for(var x in measurement) {
    console.log(measurement[x]);


    for ( var i = 0; i < data.Series.hasOwnProperty(measurement).length; i++) {

        var obj = data.Series.hasOwnProperty(measurement)[i];
        var date = obj["Date"].toString();
        var Value = obj["Value"];

        console.log(date, Value);
    }

但是我无法让它工作,循环遍历data.Series. /heights/lengths../ [i]

我非常感谢有关如何实现这一目标的提示。

谢谢!

1 个答案:

答案 0 :(得分:1)

如果您将measurement替换为data.Series并删除hasOwnProperty(measurement)内容,那么您几乎就在那里。您唯一需要的是保持从 {Date,Value} 对象列表到一对日期列表和每个系列值的转换。

var series = {};
// This loop is looping across all the series.
// x will have all the series names (heights, lengths, etc.).
for (var x in data.Series) {
  var dates = [];
  var values = [];
  // Loop across all the measurements for every serie.
  for (var i = 0; i < data.Series[x].length; i++) {
    var obj = data.Series[x][i];
    // Assuming that all the different series (heights, lengths, etc.) have the same two Date, Value attributes.
    dates.push(obj.Date);
    values.push(obj.Value);
  }
  // Keep the list of dates and values by serie name.
  series[x] = {
    dates: dates,
    values: values
  };
}

series将包含此内容:

{
    heights: {
        dates: [
            '2014-10-01',
            '2014-10-01',
            '2014-10-01'
        ],
        values: [
            22,
            53,
            57
        ]
    },
    lengths: {
        dates: [
            '2014-10-01',
            '2014-10-01'
        ],
        values: [
            54,
            33
        ]
    }
}

所以你可以像这样使用它们:

console.log(series);
console.log(series.heights);
console.log(series.heights.dates);
console.log(series.heights.values);
console.log(series.lengths);
console.log(series.lengths.dates);
console.log(series.lengths.values);