0

I am trying transform an array object with input data like this:

  var input = [
    { IdDevice: "1", Time: "11:01:00", Data: "1,12,23" },
    { IdDevice: "2", Time: "11:01:11", Data: "30,40,50" },
    { IdDevice: "1", Time: "11:02:00", Data: "5,10,21" },
    { IdDevice: "2", Time: "11:02:11", Data: "32,44,53" },
  ];

After some transforms:

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

I have the result:

[
  { data: [ '1', '5' ], name: 'device 1 item 1' },
  { data: [ '12', '10' ], name: 'device 1 item 2' },
  { data: [ '23', '21' ], name: 'device 1 item 3' },
  { data: [ '30', '32' ], name: 'device 2 item 1' },
  { data: [ '40', '44' ], name: 'device 2 item 2' },
  { data: [ '50', '53' ], name: 'device 2 item 3' }
]

And now I want the final data will depend on "Time" attribute, such as: the data: "1" is in time: "11:01:00" so x:"1", y:"11:01:00" and so on...:

[
  { data: [
    {x:'1', y:"11:01:00"},
    {x:'5', y:"11:02:00"}
  ],
    name: 'device 1 item 1' 
  },
  { data: [
    {x:'12', y:"11:01:00"},
    {x:'10', y:"11:02:00"}
  ],
    name: 'device 1 item 2' 
  },
  ...
  { data: [
    {x:'40', y:"11:01:11"},
    {x:'44', y:"11:02:11"}
  ],
    name: 'device 2 item 2' 
  },
  { data: [
    {x:'50', y:"11:01:11"},
    {x:'53', y:"11:02:11"}
  ],
    name: 'device 2 item 3' 
  }
]

How can I do this?

4
  • what is the logic behind do you want to achieve? Your expected output doesn't have '30', '32' combination compared to your output Commented May 26, 2021 at 14:15
  • the code is so long, so I cut part of the expected output Commented May 26, 2021 at 14:26
  • So what is the logic? Please update your question with the logic behind. Your question final data will depend on "Time" attribute is not clear enough. Commented May 26, 2021 at 14:27
  • I have updated the request Commented May 26, 2021 at 15:27

1 Answer 1

2

This is a very simplified answer, maybe not what you wanted, but I believe gives the end response you want.

var input = [{
    IdDevice: "1",
    Time: "11:01:00",
    Data: "1,12,23"
  },
  {
    IdDevice: "2",
    Time: "11:01:11",
    Data: "30,40,50"
  },
  {
    IdDevice: "1",
    Time: "11:02:00",
    Data: "5,10,21"
  },
  {
    IdDevice: "2",
    Time: "11:02:11",
    Data: "32,44,53"
  },
];


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({
          x: data,
          y: time
        }), a
      ), {}
    )
  ).map(([name, data]) => ({
    data,
    name
  }));
var data = convert(input);

console.log(data)

Output:

[
  {
    "data": [
      {
        "x": "1",
        "y": "11:01:00"
      },
      {
        "x": "5",
        "y": "11:02:00"
      }
    ],
    "name": "device 1 item 1"
  },
  {
    "data": [
      {
        "x": "12",
        "y": "11:01:00"
      },
      {
        "x": "10",
        "y": "11:02:00"
      }
    ],
    "name": "device 1 item 2"
  },
  {
    "data": [
      {
        "x": "23",
        "y": "11:01:00"
      },
      {
        "x": "21",
        "y": "11:02:00"
      }
    ],
    "name": "device 1 item 3"
  },
  {
    "data": [
      {
        "x": "30",
        "y": "11:01:11"
      },
      {
        "x": "32",
        "y": "11:02:11"
      }
    ],
    "name": "device 2 item 1"
  },
  {
    "data": [
      {
        "x": "40",
        "y": "11:01:11"
      },
      {
        "x": "44",
        "y": "11:02:11"
      }
    ],
    "name": "device 2 item 2"
  },
  {
    "data": [
      {
        "x": "50",
        "y": "11:01:11"
      },
      {
        "x": "53",
        "y": "11:02:11"
      }
    ],
    "name": "device 2 item 3"
  }
]
Sign up to request clarification or add additional context in comments.

6 Comments

The output is error ``` [ { data: [ [Object], [Object] ], name: 'device 1 item 1' }, { data: [ [Object], [Object] ], name: 'device 1 item 2' }, { data: [ [Object], [Object] ], name: 'device 1 item 3' }, { data: [ [Object], [Object] ], name: 'device 2 item 1' }, { data: [ [Object], [Object] ], name: 'device 2 item 2' }, { data: [ [Object], [Object] ], name: 'device 2 item 3' } ] ```
Odd, it worked in my ide. Now I’m away for the next couple hours. If it’s still unsolved, I’ll revisit then
I'm looking forward to hearing from you
@CSNo.1 - the snippet isn't throwing an error. I updated my answer to show the output I get. Where are you seeing the error?
Yah, SON.stringify(output) resolve the problem
|

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.