Chartjs-How to wrangle JSON data into arrays of unknown name

0👍

You can achieve this using some dynamic arrays:

var dataset1 = [ { "time": "2017-03-02 09:00", "volts": 10, "amps": 4.5 },{ "time": "2017-03-02 09:10", "volts": 9.87, "amps": 4.15 }];
var dataset2 = [ { "level": 1.23, "time": "2017-03-02 09:00", "volts": 3.5 }, { "level": 1.56, "time": "2017-03-02 09:10", "volts": 3.6 }];
var data = [];
var datasets = [dataset1,dataset2];
for (var i in datasets) {
  var dataset = datasets[i];
  for (var j in dataset) {
    var d = dataset[j];
    for (var k in d) {
      if (!data[k]) data[k] = [];
      data[k].push(d[k]);
    }
  }
}

Working example: https://jsfiddle.net/mspinks/zhuxz2z6/1/

Leave a comment