将json响应转换为我的要求

时间:2017-01-14 01:19:53

标签: javascript json

我收到了像

这样的JSON回复
            [
             {"no":001, "location": "England", "year":"2017", "month":"4", "amount":"1000"},
             {"no":002, "location": "Italy", "year":"2017", "month":"3", "amount":"8000"},
             {"no":001, "location": "England", "year":"2016", "month":"2", "amount":"9000"},
             {"no":001, "location": "England", "year":"2016", "month":"11","amount":"12000"}
            ];

我需要做到

            [
             {"no":001, "location": "England", "year":"2016", "amount2":"9000", "amount11":"12000"},
             {"no":001, "location": "England", "year":"2017", "amount4":"1000"},
             {"no":002, "location": "Italy", "year":"2017", "amount3":"8000"}
            ];

基于不将多个记录转换为具有特定于月份和常见信息的数量的记录,即位置但相同的no应该具有与年份相对应的不同记录。

4 个答案:

答案 0 :(得分:2)

一般建议:按ID在另一个数据结构中收集信息。我想这是一个Accumulator Pattern

var rows = [
    {"no":"001","month":"4","amount":"1000"},
    {"no":"002","month":"3","amount":"8000"},
    {"no":"001","month":"2","amount":"9000"},
    {"no":"001","month":"11","amount":"12000"}
];
var results = [];

var idToResultsObject = {};

for (var i = 0; i < rows.length; i++) {
    var row = rows[i];
    var id = row['no'];
    if (typeof idToResultsObject[id] == 'undefined')
        idToResultsObject[id] = { 'no' : id };
    idToResultsObject[id]['location'] = row['location'];
    idToResultsObject[id]['amount' + row['month']] = row['amount'];
}

for (var id in idToResultsObject) {
    results.push(idToResultsObject[id]);
}

console.log(results);

当然有一种更好的存储方式。

答案 1 :(得分:1)

这是更新的es5代码,以匹配您对问题的最新修改: (我仍然不确定我从no了解你想要什么,所以现在我把它留作number它在输入中(不在引号中)

var input = [
{"no":001, "location": "England", "year":"2017", "month":"4", "amount":"1000"},
{"no":002, "location": "Italy", "year":"2017", "month":"3", "amount":"8000"},
{"no":001, "location": "England", "year":"2016", "month":"2", "amount":"9000"},
{"no":001, "location": "England", "year":"2016", "month":"11","amount":"12000"}
];

/*
[
{"no":001, "location": "England", "year":"2016", "amount2":"9000", "amount11":"12000"},
{"no":001, "location": "England", "year":"2017", "amount4":"1000"},
{"no":002, "location": "Italy", "year":"2017", "amount3":"8000"}
];
*/

var output = input.reduce( function(result, cur) {
  var ref = result.find( function(row) {
  	return row.no === cur.no && row.location === cur.location && cur.year === row.year }
  );
  if (ref) {
  	ref["amount"+cur.month] = cur.amount;
  } else {
  	var newRow = { "no": cur.no, "location": cur.location, "year": cur.year };
    newRow["amount"+cur.month] = cur.amount;
    result.push(newRow);
  }
  return result;
},[])

console.log(output);

答案 2 :(得分:0)

正如其他人所说,设计这些对象真的是一种糟糕的方式。但是这段代码可以满足您的需求(es6语法):

&#13;
&#13;
var input = [
    {"no":001,"month":"4","amount":"1000"},
    {"no":002,"month":"3","amount":"8000"},
    {"no":001,"month":"2","amount":"9000"},
    {"no":001,"month":"11","amount":"12000"}
];

/*
[
    {"no":001, "amount2":"9000", "amount4":"1000", "amount11":"12000"},
    {"no":002, "amount3":"8000"}
];
*/

var output = input.reduce( (result, cur) => {
  var ref = result.find( row => row.no === cur.no);
  if (ref) {
  	ref["amount"+cur.month] = cur.amount;
  } else {
  	var newRow = { "no": cur.no };
    newRow["amount"+cur.month] = cur.amount;
    result.push(newRow);
  }
  return result;
},[])

console.log(output);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

es5语法

&#13;
&#13;
var input = [
    {"no":001,"month":"4","amount":"1000"},
    {"no":002,"month":"3","amount":"8000"},
    {"no":001,"month":"2","amount":"9000"},
    {"no":001,"month":"11","amount":"12000"}
];

/*
[
    {"no":001, "amount2":"9000", "amount4":"1000", "amount11":"12000"},
    {"no":002, "amount3":"8000"}
];
*/

var output = input.reduce( function(result, cur) {
  var ref = result.find( function(row) { return row.no === cur.no });
  if (ref) {
  	ref["amount"+cur.month] = cur.amount;
  } else {
  	var newRow = { "no": cur.no };
    newRow["amount"+cur.month] = cur.amount;
    result.push(newRow);
  }
  return result;
},[])

console.log(output);
&#13;
&#13;
&#13;