如何组合JSON对象与相同的键,并添加他们的其他相应的值
How to combine JSON object with same key and add their other corresponding values?
我试了下面的代码。
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"
}]
对于具有相同级别的两个对象来说,它工作得很好,但如果数组中具有相同级别的对象大于两个,则它不工作。我的要求是为具有相同级别的任何数量的对象添加值。提前感谢!
您可以使用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; }
相关文章:
- FabricJs-限制主对象内添加对象的移动区域
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 谷歌地图可以't添加正确的标记ID
- 在SVG地图上添加水的渐变,就像在谷歌地图(PHP/JS)中一样
- 将Adobe创意编辑的图像上传到我的服务器,而不是他们的创意云
- 可以在Javascript中添加新的关键字吗
- Twitter是如何在文本框中添加标签的
- 扩展Nashorn-ecma引擎以添加新的运算符和类型
- underline-js:将键对象与数组键对象进行比较,如果存在,则移除现有的添加新的
- 为什么人们添加他们的JavaScript代码
- 如何将现有 ul 的所有 li 添加到另一个 ul 的底部,基于他们的 css 类
- 有没有办法让wysithtml5'的链接编辑器允许用户向href标记添加他们喜欢的任何内容
- 如何添加图像到mysql数据库,当用户提交他们的图像
- 如何组合JSON对象与相同的键,并添加他们的其他相应的值
- 我如何使一个表单在php中,允许用户添加额外的表单字段,如果他们需要他们
- 我试图找到一种方法,让人们通过链接向他们的蒸汽购物车添加多个项目
- CSS过渡-为什么他们不关注新添加内容的类更改?
- 创建多个标签,并添加不同的文本到他们的变量
- 工作的谷歌地图API,我有它的设置,以便用户可以添加一个选择矩形到他们的屏幕.我怎么能只做一次呢
- 如何添加插座?IO套接字通过他们的socketid到一个房间