I recieve a JSON from the server like ExampleA but i need it to be like ExampleB,I tried a lot of things, like splice, push, spread operator but couldnt achieve my goal, it seems i'm overnesting the structure. ExampleA:
[
{
"year": 2017,
"Setor": {
"6": {
"ID Sector": 7,
"revenue": 5555555
},
"7": {
"ID Sector": 10,
"revenue": 5555555
}
}
},
{
"year": 2018,
"Setor": {
"8": {
"ID Sector": 7,
"revenue": 7777777
},
"9": {
"ID Sector": 10,
"revenue": 7777777
}
}
}
]
ExampleB:
[
{
"7": 5555555,
"year": 2017,
"10": 5555555,
},
{
"7": 7777777,
"year": 2018,
"10":7777777
}
]
As you can see, i want a JSON where each array have a "year", and the id of Sector as a "index" and its value as its revenue.
Edit: sorry for not including my code, this is the last thing i tried:
var Obj=[];
this.data.items.forEach((value,index,array)=>{
var data = value.data;
var arraySectorsData =[];
var actualYear = {year:data.year};
for(var sector in data.Setor){
var revenue = {[data.Setor[sector]['ID Sector']]:data.Setor[sector].revenue};
arraySectorsData.splice(0, 0, revenue);
}
arraySectorsData.splice(0, 0, actualYear);
Obj[index]=[...arraySetoresDados];
});
And this is what i'm getting:
[
[
{
"year": 2017
},
{
"7": 5555555
},
{
"10": 5555555
}
],
[
{
"year": 2018
},
{
"7": 7777777
},
{
"10": 7777777
}
]
]
,I tried a lot of things, like splice, push, spread operator but couldnt achieve my goal, it seems i'm overnesting the structurepost the code you have tried so far ?