Chartjs-Javascript get JSON into arrays, combine and use elsewhere

0👍

Here’s a way to manually mix them together…. The gist is to manually splice the strings together and then use Date.parse() to make the date objects.

var dates = [
"2016-03-13",
"2016-03-13",
"2016-03-14",
"2016-03-14",
];

var times = [
"16:41:13",
"17:36:57",
"08:53:02",
"21:53:11",
];

var both = [];
for (var i = 0; i < dates.length; i++) {
    both.push(new Date(Date.parse(dates[i] + " " + times[i])));
}

console.log("%j",both);

0👍

This sort of thing is easy if you use a library like lodash. In this case you would want _.zip

var dates = data.dates;
var times = data.times;

var zipped = _.zip(dates, times);
var text = _.reduce(zipped, function(aggregate, item) { return ',' + item[0] + ' ' + item[1]; });
console.log(text);

0👍

try splitting the dates and times with , and concat each of them by using loop

var data = {
  "dates": [
    "2016-03-13",
    "2016-03-13",
    "2016-03-14",
    "2016-03-14"
  ],
  "times": [
    "16:41:13",
    "17:36:57",
    "08:53:02",
    "21:53:11"
  ]
}

var datetimes = [];

for (var i = 0; i < data.dates.length; i++) {
  datetimes.push(data.dates[i] + ' ' + data.times[i]);
}

document.write('<pre>' + JSON.stringify(datetimes, 0, 4) + '</pre>')

Leave a comment