I've got some data:
const data = [
[
[1609459200000, 18],
[1612137600000, 12],
[1614556800000, 12],
[1617231600000, 14]
],
[
[1609459200000, 30],
[1612137600000, 501],
[1614556800000, 81],
[1617231600000, 82],
]
]
The long numbers are timestamps and I want to return an array as below:
const result = [{
x: 1609459200000,
y: 48
}, {
x: 1612137600000,
y: 513
}, {
x: 1614556800000,
y: 93
}, {
x: 1617231600000,
y: 96
}]
The X will contain the timestamp and the Y will be the sum of the values at the index 1 of each inner array, corresponding to each timestamp.
In case one of the array's element has more values than the other, it should sum it anyway as below:
const data = [
[
[1609459200000, 18],
[1612137600000, 12],
[1614556800000, 12],
[1617231600000, 14]
],
[
[1609459200000, 30],
[1612137600000, 501],
[1614556800000, 81],
]
]
const result = [{
x: 1609459200000,
y: 48
}, {
x: 1612137600000,
y: 513
}, {
x: 1614556800000,
y: 93
}, {
x: 1617231600000,
y: 14
}]
My attempts were completely worthless and couldn't pass the part where the array is multidimensional
FAILED
const totals = data.reduce((total, curr, i) => {
total = {
x: curr[0][i],
y: curr[1][i]
}
}, {})
console.log('total', totals)
EDIT
The data array can have more than 2 sub arrays.