0

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
    }
  ]
]
5
  • 3
    Your efforts so far ? ,I tried a lot of things, like splice, push, spread operator but couldnt achieve my goal, it seems i'm overnesting the structure post the code you have tried so far ? Commented Mar 1, 2019 at 17:39
  • 1
    what have you tried so far? what error messages have you received? there are many, many questions on Stack Overflow regarding manipulation of JSON - which ones have you investigated and why do they not solve your specific problem? if they do not apply, then please post your code here so we can better help you. Commented Mar 1, 2019 at 17:39
  • 1
    Remember that in S.O. you should show people that you have tried to solve the problem Commented Mar 1, 2019 at 17:40
  • Note that you don't have "a JSON". You have an array of objects. JSON is a string format. Commented Mar 1, 2019 at 17:40
  • Sorry for the inconvenience guys, I edited the post to include my code Commented Mar 1, 2019 at 18:00

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

1

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);

Comments

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.