Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Transform data in array

I need to transform data from first format to second for my ui library. But I can't completely understand how I should do this. I need to add arrays "data" each id

My data:

[
{id: 'Battery charging', time: '2021-0903T14:10:00Z', value: 3890.019022727273, table: 0}

{id: 'Battery charging', time: '2021-09-03T14:15:00Z', value: 3594.3097145454544, table: 0}
{id: 'Battery charging', time: '2021-09-03T14:20:00Z', value: 4069.6454163636363, table: 0}

{id: 'Battery charging', time: '2021-09-03T14:25:00Z', value: 4090.7089309090907, table: 0}

{id: 'Battery charging', time: '2021-09-03T14:30:00Z', value: 3530.3841, table: 0}

{id: 'Battery charging', time: '2021-09-03T14:35:00Z', value: 4154.7032509090905, table: 0}

{id: 'Battery charging', time: '2021-09-03T14:40:00Z', value: 4752.12578, table: 0}
{id: 'Battery charging', time: '2021-09-03T14:45:00Z', value: 5906.133650000001, table: 0}

{id: 'Battery charging', time: '2021-09-03T14:50:00Z', value: 4148.342200000001, table: 0}

{id: 'Battery discharging', time: '2021-09-03T14:10:00Z', value: 0, table: 1}

{id: 'Battery discharging', time: '2021-09-03T14:15:00Z', value: 0, table: 1}

{id: 'Battery discharging', time: '2021-09-03T14:20:00Z', value: 0, table: 1}

{id: 'Battery discharging', time: '2021-09-03T14:25:00Z', value: 0, table: 1}

{id: 'Battery discharging', time: '2021-09-03T14:30:00Z', value: 0, table: 1}

{id: 'Battery discharging', time: '2021-09-03T14:35:00Z', value: 0, table: 1}

{id: 'Battery discharging', time: '2021-09-03T14:40:00Z', value: 0, table: 1}

{id: 'Battery discharging', time: '2021-09-03T14:45:00Z', value: 0, table: 1}

{id: 'Battery discharging', time: '2021-09-03T14:50:00Z', value: 45.93099, table: 1}

]

Need to transform to this:

[
{id: 'Battery charging', 
data: [
{time: '2021-09-03T14:10:00Z', value: 3890.019022727273}

{time: '2021-09-03T14:15:00Z', value: 3594.3097145454544}

{time: '2021-09-03T14:20:00Z', value: 4069.6454163636363}

{time: '2021-09-03T14:25:00Z', value: 4090.7089309090907}

{time: '2021-09-03T14:30:00Z', value: 3530.3841}

{time: '2021-09-03T14:35:00Z', value: 4154.7032509090905}

{time: '2021-09-03T14:40:00Z', value: 4752.12578}
{time: '2021-09-03T14:45:00Z', value: 5906.133650000001}

{time: '2021-09-03T14:50:00Z', value: 4148.342200000001}
]

{id: 'Battery discharging',
data: [
{time: '2021-09-03T14:10:00Z', value: 0}
{
time: '2021-09-03T14:15:00Z', value: 0}
{
time: '2021-09-03T14:20:00Z', value: 0}

{time: '2021-09-03T14:25:00Z', value: 0}

{time: '2021-09-03T14:30:00Z', value: 0}
{
time: '2021-09-03T14:35:00Z', value: 0}
{
time: '2021-09-03T14:40:00Z', value: 0}
{
time: '2021-09-03T14:45:00Z', value: 0}

{time: '2021-09-03T14:50:00Z', value: 45.93099}
]
]

Answer*

Draft saved
Draft discarded
Cancel
1
  • Thank you a lot! I used exactly your code!) Commented Sep 25, 2021 at 20:42

lang-js

AltStyle によって変換されたページ (->オリジナル) /