如何将JSON对象与相同的键组合并添加其他对应的值?

最后发布: 2016-11-17 19:48:06


问题

我尝试了以下代码。

var SeatWithCat = [{
  "level": "Level II",
  "price": 5,
  "quantity": 1,
  "seats": "B3"
}, {
  "level": "Level II",
  "price": 5,
  "quantity": 1,
  "seats": "B1"
}, {
  "level": "Level I",
  "price": 10,
  "quantity": 1,
  "seats": "A2"
}, {
  "level": "Level III",
  "price": 30,
  "quantity": 1,
  "seats": "C1"
}, {
  "level": "Level III",
  "price": 30,
  "quantity": 1,
  "seats": "C2"
}, {
  "level": "Level V",
  "price": 50,
  "quantity": 1,
  "seats": "E1"
}, {
  "level": "Level II",
  "price": 5,
  "quantity": 1,
  "seats": "B2"
}, {
  "level": "Level VI",
  "price": 2,
  "quantity": 1,
  "seats": "F1"
}];
var temp = [];
var jsonarr = [];
for (var i = 0; i < SeatWithCat.length; i++) {
  for (var j = 1; j < SeatWithCat.length; j++) {
    if (SeatWithCat[i].level === SeatWithCat[j].level) {
      temp.push({
        level: SeatWithCat[i].level,
        quantity: SeatWithCat[i].quantity + SeatWithCat[j].quantity,
        price: SeatWithCat[i].price + SeatWithCat[j].price,
        seats: SeatWithCat[i].seats + "," + SeatWithCat[j].seats
      });
      SeatWithCat = SeatWithCat.filter(function(el) {
        return el.level !== SeatWithCat[i].level;
      });
      jsonarr = SeatWithCat;
      alert(JSON.stringify(temp));
    }
  }
}
var finalObj = temp.concat(jsonarr);
alert(JSON.stringify(finalObj));

输出:

[{
  "level": "Level II",
  "quantity": 2,
  "price": 10,
  "seats": "B3,B1"
}, {
  "level": "Level III",
  "quantity": 2,
  "price": 60,
  "seats": "C1,C1"
}, {
  "level": "Level VI",
  "quantity": 2,
  "price": 4,
  "seats": "F1,F1"
}, {
  "level": "Level I",
  "price": 10,
  "quantity": 1,
  "seats": "A2"
}, {
  "level": "Level V",
  "price": 50,
  "quantity": 1,
  "seats": "E1"
}]

对于具有相同级别的两个对象,它工作正常,但如果数组中具有相同级别的对象多于两个,则它不起作用。 我的要求是为具有相同级别的任意数量的对象添加值。 提前致谢!

javascript json
回答

您可以使用Array.prototype.reduce()收集字典中的唯一项,然后使用Array.prototype.map()将字典转换回数组:

 function combine(arr) { var combined = arr.reduce(function(result, item) { var current = result[item.level]; result[item.level] = !current ? item : { level: item.level, price: current.price + item.price, quantity: current.quantity + item.quantity, seats: current.seats + ',' + item.seats }; return result; }, {}); return Object.keys(combined).map(function(key) { return combined[key]; }); } var SeatWithCat = [{"level":"Level II","price":5,"quantity":1,"seats":"B3"},{"level":"Level II","price":5,"quantity":1,"seats":"B1"},{"level":"Level I","price":10,"quantity":1,"seats":"A2"},{"level":"Level III","price":30,"quantity":1,"seats":"C1"},{"level":"Level III","price":30,"quantity":1,"seats":"C2"},{"level":"Level V","price":50,"quantity":1,"seats":"E1"},{"level":"Level II","price":5,"quantity":1,"seats":"B2"},{"level":"Level VI","price":2,"quantity":1,"seats":"F1"}]; var result = combine(SeatWithCat); console.log(result); 


回答

您可以使用哈希表作为结果集中相同级别对象的引用。

迭代数组并检查哈希-如果未设置,则生成具有实际属性的新对象。 否则增加quantity并增加seats

该提议仅使用一个循环。

 var seatWithCat = [{ level: "Level II", price: 5, quantity: 1, seats: "B3" }, { level: "Level II", price: 5, quantity: 1, seats: "B1" }, { level: "Level I", price: 10, quantity: 1, seats: "A2" }, { level: "Level III", price: 30, quantity: 1, seats: "C1" }, { level: "Level III", price: 30, quantity: 1, seats: "C2" }, { level: "Level V", price: 50, quantity: 1, seats: "E1" }, { level: "Level II", price: 5, quantity: 1, seats: "B2" }, { level: "Level VI", price: 2, quantity: 1, seats: "F1" }], result = []; seatWithCat.forEach(function (o) { if (!this[o.level]) { this[o.level] = { level: o.level, price: o.price, quantity: o.quantity, seats: o.seats }; result.push(this[o.level]); return; } this[o.level].quantity += o.quantity; this[o.level].seats += ',' + o.seats; }, Object.create(null)); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 


回答

我强烈建议您使用lodash及其出色的瑞士军刀工具,而不要自己编写。 您可能需要更多。 https://lodash.com/docs/4.16.4