在JavaScript中重组JSON?

时间:2019-03-01 17:37:56

标签: javascript json

我从ExampleA这样的服务器上接收到JSON,但我需要像ExampleB这样,我尝试了很多操作,例如splice,push,spread运算符,但无法实现我的目标,看来我在覆盖结构。 示例A:

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

如您所见,我想要一个JSON,其中每个数组都有一个“年”,Sector的ID作为“索引”,其值作为其收入。

编辑:很抱歉不包含我的代码,这是我尝试的最后一件事:

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

这就是我得到的:

[
  [
    {
      "year": 2017
    },
    {
      "7": 5555555
    },
    {
      "10": 5555555
    }
  ],
  [
    {
      "year": 2018
    },
    {
      "7": 7777777
    },
    {
      "10": 7777777
    }
  ]
]

3 个答案:

答案 0 :(得分:2)

一种解决方案是使用 Array.map() 将元素映射到新对象。为了创建新对象,我们使用 Object.assign() Object.values() 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;}

答案 1 :(得分:1)

您可以将嵌套的 map reduce Object.values()一起使用,如下所示:

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)

答案 2 :(得分:1)

类似于Shidersz解决方案,但在import osmnx as ox outline_city = ox.gdf_from_place({'city':'Salzburg'}) outline_state = ox.gdf_from_place({'state':'Salzburg'}) 结果上使用了扩展语法:

map()