vendredi 18 juin 2021

Add if condition to Function JavaScript

I have an input:

var modbusData = [
  { Type: "rtu", Mode: "master", Device: "1", IdDevice: "1", Time: "11:01:00", Data: "1,12,23" },
  { Type: "tcp", Mode: "client", Device: "1", IdDevice: "2", Time: "11:01:11", Data: "30,40,50" },
  { Type: "rtu", Mode: "slave", Device: "2", IdDevice: "1", Time: "11:02:00", Data: "5,10,21" },
  { Type: "tcp", Mode: "server", Device: "2", IdDevice: "2", Time: "11:02:11", Data: "32,44,53" },
];

How can I add if condition into this function in order to: floatMap({ IdDevice, Data, Time }) when Type: "rtu" and floatMap({ Device, Data, Time }) when Type: "tcp"

const convert = (input) =>
Object.entries(
  input
    .flatMap(({ IdDevice, Data, Time }) =>
      Data.split(", ").map((x, i) => ({
        data: x,
        name: `device ${IdDevice} item ${i + 1}`,
        time: Time,
      }))
    )
    .reduce(
      (a, { data, name, time }) => (
        (a[name] = a[name] || []),
        a[name].push({
          y: data,
          x: time,
        }),
        a
      ),
      {}
    )
).map(([name, data]) => ({
  data,
  name,
}));

Aucun commentaire:

Enregistrer un commentaire