在JSON中分组相似的项目

时间:2016-04-13 20:33:41

标签: javascript laravel

你好我有一个像JSON这样的集合:

[
 {
   "quantity": "1",
   "description": "VIP Ticket to Event"
 },
 {
   "quantity": "1",
   "description": "VIP Ticket to Event"
 },
 {
   "quantity": "1",
   "description": "VIP Ticket to Event"
 },
 {
   "quantity": "1",
   "description": "Regular Ticket to Event"
 },
 {
   "quantity": "1",
   "description": "Regular Ticket to Event"
 },
]

但是我想将类似的项目组合在一起,所以最后我有这个:

[
 {
   "quantity": "3",
   "description": "VIP Tickets to Event"
 },
 {
   "quantity": "2",
   "description": "Regular Tickets to Event"
 }
]

这很不幸但我不能改变数据库结构,所以我唯一能做的就是玩弄结果。做这种分组的好方法是什么?

这被标记为laravel和javascript,因为我需要将相同的解决方案应用到我的前端。

4 个答案:

答案 0 :(得分:0)

您可以使用reduce创建一个包含更新结构的新数组 - 按description分组

var grouped = arr.reduce(function(groups, current) {
    var found = false;
    for (var i = 0; i < groups.length; i++) {
        if (groups[i].description == current.description) {
            groups[i].quantity++;
            found = true;
        }
    }

    if (!found) {
        groups.push(current);
    }

    return groups;
}, []);

答案 1 :(得分:0)

这是一个带有临时对象的提案,用于引用具有相同描述的对象。

var array = [{ "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "Regular Ticket to Event" }, { "quantity": "1", "description": "Regular Ticket to Event" }, ],
    grouped = [];

array.forEach(function (a) {
    if (!this[a.description]) {
        this[a.description] = { description: a.description, quantity: 0 };
        grouped.push(this[a.description]);
    }
    this[a.description].quantity += +a.quantity;
}, {});

document.write('<pre> ' + JSON.stringify(grouped, 0, 4) + '</pre>');

答案 2 :(得分:0)

使用临时对象

&#13;
&#13;
var d = [{ "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "VIP Ticket to Event" }, { "quantity": "1", "description": "Regular Ticket to Event" }, { "quantity": "1", "description": "Regular Ticket to Event" }]; 

var o = {};

d.forEach(e => o[e.description] = o[e.description] + +e.quantity || +e.quantity);

var r = Object.keys(o).map(e => ({ quantity: o[e], description: e }));
  
document.write('<pre> ' + JSON.stringify(r, 0, 2) + '</pre>');;
&#13;
&#13;
&#13;

答案 3 :(得分:0)

另一种reduce,您不必为输入数组中的每个项迭代您的groups数组以检查现有项。

使用简化值本身作为hashMap来检查重复

var groupedItemsObj = items.reduce(function(previous, next){
  if(previous[next.description]){
      var updatedQuantity = previous[next.description].quantity +   next.quantity;  
      previous[next.description] = {quantity: updatedQuantity, description: next.description};
  }else{
      previous[next.description] = next;
  }
  return previous;
}, {});

最后使用map将缩小的对象转换为数组。

var groupedItemsArray = Object.keys(groupedItemsObj).map(function(key){
    return groupedItemsObj[key];
}); 

检查工作箱here。我把数量改为数字。还将一些值更改为2以显示其正常工作。