I have "available" data and want to convert it into "desired".
I tried to merge all objects inside array using this but its not working coz as you can see for timestamp = 10 we have 2 values of sw_version
const available = [
{
"timestamp": 10,
"sw_version": "AA"
},
{
"timestamp": 10,
"sw_version": "AB"
},
{
"timestamp": 20,
"sw_version": "QFX-1.2.5 B"
},
{
"timestamp": 10,
"pressure": 14.75
},
{
"timestamp": 20,
"pressure": 14.22
},
{
"timestamp": 10,
"temperature": 15.96
},
{
"timestamp": 20,
"temperature": 38.50
},
{
"timestamp": 30,
"temperature": 2.2
},
{
"timestamp": 30,
"pressure": 9.8
}
]
const desired = [
{
"timestamp": 10,
"sw_version": "AA",
"pressure": 14.75,
"temperature": 15.96
},
{
"timestamp": 10,
"sw_version": "AB",
"pressure": 14.75,
"temperature": 15.96
},
{
"timestamp": 20,
"sw_version": "QFX-1.2.5 B",
"pressure": 14.22,
"temperature": 38.5
},
{
"timestamp": 30,
"pressure": 9.8,
"temperature": 2.2
}
]
const output = available.reduce((result, item) => {
const i = result.findIndex((resultItem) => resultItem.timestamp === item.timestamp);
if (i === -1) {
result.push(item);
} else {
result[i] = { ...result[i], ...item };
}
return result;
}, []);
console.log(output)
The output was
[
{
"timestamp": 10,
"sw_version": "AB",
"pressure": 14.75,
"temperature": 15.96
},
{
"timestamp": 20,
"sw_version": "QFX-1.2.5 B",
"pressure": 14.22,
"temperature": 38.5
},
{
"timestamp": 30,
"temperature": 2.2,
"pressure": 9.8
}
]
As you can see in the desired output i want 2 objects with timestamp = 10 but in the function output its is overwriting the first one and keeping only one object.
We will get multiple values for same timestamp for only one measurement which is sw_version. For other measurements like temperature or pressure we won't get multiple values for same timestamps. sw_version is optional as well so we may get data where sw_version will not be there and we just have to merge without thinking about duplicating rows timestamp is mandatory it will always be there
pressureortemperaturefor a timestamp where you already have a measurement? Do you want to handle this the same way as forsw_versioni. e. also have a separate object in the output for those? What happens if you receive the same measurement for the same timestamp twice e.g.m1 = { timestamp: 10, temp: 10 }andm2 = { timestamp: 10, temp: 10 }. Should these be one or two objects in the output?sw_versionare always the first in your initial array ?timestamporsw_versionproperties ?