Is it possible to parse through this list and create a new nested object for every sensor after "RECORD".
{
"TIMESTAMP": "2022-10-21 13:55:00",
"RECORD": "0",
"Sensor1": "1.654",
"Sensor2": "1.176",
"Sensor3": "0.706"
}
Result:
{
"Sensor1": {
"TIMESTAMP": "2022-08-21 13:55:00",
"Value": "1.654"
},
"Sensor2": {
"TIMESTAMP": "2022-08-21 13:55:00",
"Value": "1.176"
},
"Sensor3": {
"TIMESTAMP": "2022-08-21 13:55:00",
"Value": "0.706"
}
}
3
Answers
Just loop through the object keys, ignore the
TIMESTAMP
andRECORD
keys, and each other key just create its according object with the required values:You can try reducing it with
Object.entries(...).reduce(...);
. Example below:Use destructuring to get TIMESTAMP from the original object.
Use Object.entries to get [key, value] pairs from the data. Then, use filter to keep only entries whose key starts with ‘Sensor’.
Finally, map each sensor value to a new object that includes TIMESTAMP from the original object.