Before I start, yes I googled like crazy and read many answers on SO, but I just wasn't able to accomplish what needs to be done so I'm seeking help.
I have this problem that I just cannot solve. I have to display some data in a Google Annotation chart. The data I'm getting is in this format:
var arr1 = [{
timestamp: "1505735024496", value1: 2
},
{
timestamp: "1505815355920", value1: 5
},
.....
]
var arr2 = [{
timestamp: "1505383687151", value2: 1
},
{
timestamp: "1505485417374", value2: 3
},
.....
]
The end goal here is to combine these two arrays of objects into an array of arrays like this:
var final = [
[new Date(YYYY, DD, MM), value1, value2],
[new Date(YYYY, DD, MM), value1, value2],
...
]
as per instructions on the Google Charts documentation for that particular chart.
I only managed to convert timestamps to full dates with corresponding values for those dates, but that again gives me array of objects where I just converted the timestamp to a date and nothing much changed.
var indexesArr1 = arr1.map(function (obj) {
return obj.timestamp;
});
var arr1New = arr1.map(function (obj) {
var index = indexesArr1.indexOf(obj.timestamp);
return { date: moment(parseInt(obj.timestamp)).format('YYYY, MM, DD'), value1: index > -1 ? arr1[index].value : obj.value };
});
The real problem here is that arr1 and arr2 don't have to be the same length (and probably won't be most of the time) and since this chart will display data over time, it's important to populate every date with both value1 and value2, even thought one of those values might not be explicitly returned for that particular day. And one more thing would be to remove all instances of duplicate dates, but leave the last one, since that one represents the last logged value for that particular date.
I've used Moment.js to easily convert timestamps to desired format('YYYY, DD, MM'), but I just cannot do all the data manipulation properly.
Use of Underscore.js is allowed since I know it has some great utility functions for these types of problems, but I just wasn't able to solve it (don't have that much experience with it to be honest) and I know there are people here with more knowledge than me so I'm seeking help.
Thank you in advance for taking your time to answer this.
P.S. I would link to Moment.js but I don't have enough reputation to post more than 2 links currently.