I have a process mapping and splitting like this:
var input = [{Sensor:"DHT11", Date:"28/05/2021", Time:"11:00:00", Data:"Humidity: 60.1, Temperature: 30.2"},
{Sensor:"Piezo", Date:"28/05/2021", Time:"11:00:10", Data:"Vibration level: 10"},
{Sensor:"PEM-004T", Date:"28/05/2021", Time:"11:00:20", Data:"Voltage: 220.3, Current: 0.13"},
{Sensor:"DHT11", Date:"28/05/2021", Time:"11:01:00", Data:"Humidity: 60.2, Temperature: 30.0"},
{Sensor:"Piezo", Date:"28/05/2021", Time:"11:01:10", Data:"Vibration level: 20"},
{Sensor:"PEM-004T", Date:"28/05/2021", Time:"11:01:20", Data:"Voltage: 220.5, Current: 0.16"}
]
const convert = (input) =>
Object.entries(
input
.flatMap(({ Data, Time }) =>
Data.split(", ").map((x, i) => ({
data: x,
time: Time,
}))
)
)
console.log(convert(input))
And now I want to split the Data key's values on the comma separator producing a new entry which on itself will be split on a new key/value pair, while keeping the timestamp association for each resulting entry:
[
[ '0', { name: 'Humidity', data: 60.1', time: '11:00:00' } ],
[ '1', { name: 'Temperature', data: 30.2', time: '11:00:00' } ],
[ '2', { name: 'Vibration level', data: 10', time: '11:00:10' } ],
[ '3', { name: 'Voltage', data: 220.3', time: '11:00:20' } ],
[ '4', { name: 'Current', data: 0.13', time: '11:00:20' } ],
[ '5', { name: 'Humidity', data: 60.2', time: '11:01:00' } ],
[ '6', { name: 'Temperature', data: 30.0', time: '11:01:00' } ],
[ '7', { name: 'Vibration level', data: 20', time: '11:01:10' } ],
[ '8', { name: 'Voltage', data: 220.5', time: '11:01:20' } ],
[ '9', { name: 'Current', data: 0.16', time: '11:01:20' } ]
]
How could I achieve this functionally?
x => { /* ... commands here ... */ return { name: ..., ... }; }