0

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?

3
  • 1
    And the problem is? All you need to know is already in your question. Commented May 28, 2021 at 7:34
  • If you don't use a short arrow function, you can easily add statements before constructing the new object: x => { /* ... commands here ... */ return { name: ..., ... }; } Commented May 28, 2021 at 7:35
  • I want to split the first result to the final result as I posted Commented May 28, 2021 at 7:39

2 Answers 2

1

you need to split your 'x' data into the desired values

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) => {
          const [nameval,dataval] = x.split(':');
        return {
          name: nameval,
          data:dataval,
          time: Time,
        };
      })
    )
  );
    
    console.log(convert(input))

Sign up to request clarification or add additional context in comments.

Comments

0

You were basically there. Just a little adjustment in the last map() and a Number conversion on the data

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) => {
        x = x.split(":");

        return {
          name: x[0],
          data: Number(x[1]),
          time: Time,
        }
      })
    )
  )

console.log(convert(input))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.