Skip to content
Advertisement

Merging non-matching timeseries arrays in JS?

My question is of data manipulation.

Say I have multiple data arrays with the structure as follows:

JavaScript

With real data it could look e.g. like this:

JavaScript

The I need to merge multiple of such arrays into one, which is is easy if the time axes are matching. If I have e.g. three short sample arrays:

JavaScript

they would be merged into the following array, retaining the common time axis:

JavaScript

This is straightforward, but if the arrays are of different lengths where some arrays have a data point on a timestamp where others do not, what would be most efficient way of performing such parsing? I’m asking for an efficient solution as there can be a lot of data and slow parsing would result in a bad user experience. Also, the timestamps should be in order in the resulting array. It can be expected that within a single array the timestamps are in order.

Sample data for the problem I’m trying to solve:

JavaScript

And the desired result would be:

JavaScript

There is no need for interpolation, so if a timestamp does not exist in any data array, there need not be any entry in the result array of that timestamp.

Advertisement

Answer

The below should solve the problem resonably efficiently:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement