AngularJS / Angular 2:获取'http'响应后重构json

时间:2017-09-15 17:45:03

标签: angularjs json angular

当前

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

]

2 个答案:

答案 0 :(得分:0)

如果只想传入已传入的属性(例如,如果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;
});

答案 1 :(得分:0)

这是我的版本!

请注意,您想要的输出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);