【问题标题】:AngularJS/Angular 2: Restructure the json after getting 'http' responseAngularJS/Angular 2:在获得“http”响应后重构 json
【发布时间】:2017-09-15 17:45:03
【问题描述】:

当前:

[{
  "name": "a1",
  "category": "C1",
  "amount": 10
},

{
  "name": "a3",
  "category": "C1",
  "amount": 30
},

{
  "name": "a2",
  "category": "C1",
  "amount": 20
},

{
  "name": "a1",
  "category": "C2",
  "amount": 100
},

{
  "name": "a6",
  "category": "C2",
  "amount": 600
},

{
  "name": "a2",
  "category": "C2",
  "amount": 200
},

{
  "name": "a5",
  "category": "C2",
  "amount": 500
},

{
  "name": "a1",
  "category": "C3",
  "amount": 1000
},

{
  "name": "a3",
  "category": "C3",
  "amount": 3000
},

{
  "name": "a5",
  "category": "C3",
  "amount": 5000
}

]

转换为:

[

  {
    "name": "a1",
    "C1": 10,
    "C2": 100,
    "C3": 1000
  },

  {
    "name": "a2",
    "C1": 20,
    "C2": 200,
    "C3": -
  },

  {
    "name": "a3",
    "C1": 30,
    "C2": -,
    "C3": 3000
  },

  {
    "name": "a5",
    "C1": -,
    "C2": 500,
    "C3": 5000
  },

  {
    "name": "a6",
    "C1": -,
    "C2": 600,
    "C3": -
  }

]

【问题讨论】:

标签: angularjs json angular


【解决方案1】:

如果您只想要已传入的属性(例如,如果 a1 没有 C2,则映射的 a1 将没有 C2 属性):

var response = {} //set this equal to your original response, rather than an empty object
var list = {}
var mappedResponse = [];

let mapResponse = () => {
    response.forEach(item => {
        current = list[item.name] || {};
        current.name = item.name;
        current[item.category] = item.amount;
        list[item.name] = current;
    });

    Object.keys(list).forEach(key => {
        mappedResponse.push(list[key]);
    });

    return mappedResponse;
}
mapResponse();

如果您想为这些空字段显式声明类似 null 的内容,而不是让它们未定义,您可以将 forEach 更改为如下所示:

response.forEach(item => {
    current = list[item.name] || {};
    current.name = item.name;
    current['C1'] = item.category === 'C1' ? item.amount : current['C1'] || null;
    current['C2'] = item.category === 'C2' ? item.amount : current['C2'] || null;
    current['C3'] = item.category === 'C3' ? item.amount : current['C3'] || null;
    list[item.name] = current;
});

【讨论】:

    【解决方案2】:

    这是我的版本!

    请不要在您想要的输出 JSON 中包含以下行。

    "C3": -
    

    我在JSONLint.com 上检查了 JSON 是否有效,并收到以下错误。

    错误:第 14 行解析错误:... "C2": 200, "C3": - }, { "name": ----------------------^ 期望 'STRING'、'NUMBER'、'NULL'、'TRUE'、'FALSE'、'{'、'[ ',得到'未定义'

    这将使 JSON 无效。所以建议忽略这个属性!请告诉我这样好吗?

    //starting JSON
    var json = [{
      "name": "a1",
      "category": "C1",
      "amount": 10
    },
    
    {
      "name": "a3",
      "category": "C1",
      "amount": 30
    },
    
    {
      "name": "a2",
      "category": "C1",
      "amount": 20
    },
    
    {
      "name": "a1",
      "category": "C2",
      "amount": 100
    },
    
    {
      "name": "a6",
      "category": "C2",
      "amount": 600
    },
    
    {
      "name": "a2",
      "category": "C2",
      "amount": 200
    },
    
    {
      "name": "a5",
      "category": "C2",
      "amount": 500
    },
    
    {
      "name": "a1",
      "category": "C3",
      "amount": 1000
    },
    
    {
      "name": "a3",
      "category": "C3",
      "amount": 3000
    },
    
    {
      "name": "a5",
      "category": "C3",
      "amount": 5000
    }
    
    ]
    var out=[];
    var final = [];
    //code used to filter out the unique names ["a1", "a2", etc]
    for (let a of json){
      if(out.indexOf(a.name) === -1){
       out.push(a.name);
      }
    }
    //loop through the unique names!
    for (let a of out){
      var obj = {};
      //filter the object for a particular name
      for(let j of json.filter(function(x){return x.name===a})){
        //merge the objects `category` and `amount` as a object
        Object.assign(obj, {[j.category]: j.amount});
      }
      //finally push it to the final list that is `final`
      var temp = {name: a};
      Object.assign(temp, obj);
      final.push(temp);
    }
    //console.log the output
    console.log(final);

    【讨论】:

      猜你喜欢
      • 2019-02-01
      • 1970-01-01
      • 2017-03-05
      • 2017-12-20
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      相关资源
      最近更新 更多