Matheus Hatje
Matheus Hatje

Reputation: 987

Restructure JSON in JavaScript?

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
    }
  ]
]

Upvotes: 0

Views: 141

Answers (3)

adiga
adiga

Reputation: 35202

You could use a nested map and reduce with Object.values() like this:

const input = [{"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}}}]

const output = input.map(({ year, Setor }) => {
  return Object.values(Setor).reduce((r, { ["ID Sector"]: sector, revenue }) => {
           r.year = year
           r[sector] = revenue;
           return r
        }, {});
}, {})

console.log(output)

Upvotes: 1

trincot
trincot

Reputation: 349956

Similar to Shidersz solution, but with spread syntax on a map()-result:

this.data = { items: [{"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}}}]};

const obj = this.data.items.map(({year, Setor}) => 
    Object.assign({year}, ...Object.values(Setor).map(sector => 
        ({ [sector["ID Sector"]]: sector.revenue })
    ))
);

console.log(obj);

Upvotes: 1

Shidersz
Shidersz

Reputation: 17190

One solution is to use Array.map() to map your elements to new objects. For create the new objects we use Object.assign(), Object.values() and Array.reduce():

const input = [
  {
    "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}
    }
  }
];

let res = input.map(
    ({year, Setor}) => Object.assign(
        {year},
        Object.values(Setor).reduce(
            (acc, o) => (acc[[o["ID Sector"]]] = o.revenue, acc),
            {}
        )
    )
);

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Upvotes: 2

Related Questions