我有一个如下的Javascript数组

var data = [{
            "Ticket_id": "239248",
            "Order_Issue": "SAP",
            "Region": "EU",
            "Line_No": "10",
            "Line_Issue": "Qty not available",
            "Serial_Number": "72CEP92"
        },
        {
            "Ticket_id": "239248",
            "Order_Issue": "SAP",
            "Region": "EU",
            "Line_No": "20",
            "Line_Issue": "contact info missing",
            "Serial_Number": "2198IE3"
        }];

我想按多个键对数组进行分组。结果应如下所示:
var data = [{
            "Ticket_id": "239248",
            "Order_Issue": "SAP",
            "Region": "EU",
            "Lines" : [
            {"Line_No": "10",
            "Line_Issue": "Qty not available",
            "Serial_Number": "72CEP92"},
            {"Line_No": "20",
            "Line_Issue": "contact info missing",
            "Serial_Number": "2198IE3"}]
        }];

因此,基本上,我将按Ticket_id,Order_Issue和Region分组。我将添加Items子数组,在其中存储所有Line_No,Line_Issue和Serial_Number。

我成功地仅按一个键分组,比如下面的示例Ticket_id。当我向我的对象obj[item."Order_Issue] = obj[item."Order_Issue] || [];添加另一个键时,group by将仅在该键上完成,而忽略前一个键。任何建议都请问我在做什么错以及如何获取我想要的确切格式。谢谢。

var data = [{
            "Ticket_id": "239248",
            "Order_Issue": "SAP",
            "Region": "EU",
            "Line_No": "10",
            "Line_Issue": "Qty not available",
            "Serial_Number": "72CEP92"
        },
        {
            "Ticket_id": "239248",
            "Order_Issue": "SAP",
            "Region": "EU",
            "Line_No": "20",
            "Line_Issue": "contact info missing",
            "Serial_Number": "2198IE3"
        },
        {
            "Ticket_id": "239267",
            "Order_Issue": "Online Payment",
            "Region": "EU",
            "Line_No": "10",
            "Line_Issue": "card expired",
            "Serial_Number": "21BBF03"
        }];

 var group_Line_No = data.reduce(function (obj, item) {
    obj[item.Ticket_id] = obj[item.Ticket_id] || [];
    obj[item.Ticket_id].push(item.Line_No);
    return obj;
}, {});

console.log(group_Line_No);

var groups = Object.keys(group_Line_No).map(function (key) {
    return {Ticket_id: key, Line_No: group_Line_No[key]};
});

console.log(groups);

最佳答案

使用类似的方法,但是将所有键属性组合到中间结果对象的键中。

var data = [{
    "Ticket_id": "239248",
    "Order_Issue": "SAP",
    "Region": "EU",
    "Line_No": "10",
    "Line_Issue": "Qty not available",
    "Serial_Number": "72CEP92"
  },
  {
    "Ticket_id": "239248",
    "Order_Issue": "SAP",
    "Region": "EU",
    "Line_No": "20",
    "Line_Issue": "contact info missing",
    "Serial_Number": "2198IE3"
  },
  {
    "Ticket_id": "239267",
    "Order_Issue": "Online Payment",
    "Region": "EU",
    "Line_No": "10",
    "Line_Issue": "card expired",
    "Serial_Number": "21BBF03"
  }
];

var group_Line_No = data.reduce(function(obj, item) {
  let key = `${item.Ticket_id}.${item.Order_Issue}.${item.Region}`;
  obj[key] = obj[key] || {
    Ticket_id: item.Ticket_id,
    Order_Issue: item.Order_Issue,
    Region: item.Region,
    Lines: []
  };
  obj[key].Lines.push({
    Line_No: item.Line_No,
    Line_Issue: item.Line_Issue,
    Serial_Number: item.Serial_Number
  });
  return obj;
}, {});

var groups = Object.values(group_Line_No);

console.log(groups);

关于javascript - 如何基于Javascript/Jquery中的多个键对数组项进行分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60157014/

10-11 14:18