score:1

Accepted answer

if you replace measurement with data.series and get rid of the hasownproperty(measurement) thing, you are almost there. the only thing you need is a way to keep the transformation from a list of {date, value} objects to a pair of list of dates and value for each serie.

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 will contain this:

{
    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
        ]
    }
}

so you can use them like this:

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);

Related Query

More Query from same tag