I get data like this
[
{
"channel_number": 1,
"id_channel": "св-1312",
"consumption": 1231,
"date": 01
},
{
"channel_number": 1,
"id_channel": "св-1312",
"consumption": 1234,
"date": 02
},
{
"channel_number": 1,
"id_channel": "св-1312",
"consumption": 1234,
"date": 03
},
{
"channel_number": 2,
"id_channel": "св-1314",
"consumption": 800,
"date": 01
},
{
"channel_number": 2,
"id_channel": "св-1314",
"consumption": 823,
"date": 02
},
{
"channel_number": 2,
"id_channel": "св-1314",
"consumption": 1233,
"date": 03
}
]
I need to make the data for each date be merged into one object.
Like this:
[
{
date: 01,
channels: [
{
channel_number: 1
consumption: 1000
},
{
channel_number: 2
consumption: 2000
}
]
},
{
date: 02,
channels: [
{
channel_number: 1
consumption: 1000
},
{
channel_number: 2
consumption: 2000
}
]
},
...
]
The data comes from the server, so there may be more dates and channels.
I did the following but it doesn't give me what I need:
let newArr = arr.map((el: any, i: any) => {
return {
date: el.date,
channels: [{ channel_number: el.channel_number, consumption: el.consumption }],
};
});
I've tried other ways, but it doesn't work. It's just that for the first time such a transformation was needed, but I have no experience
Please, help me.