如何基于Javascript中的多个键对对象数组进行分组?

时间:2019-02-11 05:44:15

标签: javascript arrays object

我有如下数组

 [
  {
    "WarehouseId": 1,
    "ShippingCarrierId": 1,
    "PostalCodeType": "ShipToCustomer",
    "TimeStart": "1970-01-01T06:00:00.000Z",
    "TimeEnd": "1970-01-01T15:59:00.000Z",
    "PickupTimeSlot": "PM",
    "DaysToAdd": 0,
    "PickupTime": "1970-01-01T17:00:00.000Z"
  },
  {
    "WarehouseId": 1,
    "ShippingCarrierId": 1,
    "PostalCodeType": "ShipToCustomer",
    "TimeStart": "1970-01-01T16:00:00.000Z",
    "TimeEnd": "1970-01-01T23:59:00.000Z",
    "PickupTimeSlot": "AM",
    "DaysToAdd": 1,
    "PickupTime": "1970-01-01T11:00:00.000Z"
  },
  {
    "WarehouseId": 1,
    "ShippingCarrierId": 1,
    "PostalCodeType": "ShipToCustomer",
    "TimeStart": "1970-01-01T00:00:00.000Z",
    "TimeEnd": "1970-01-01T05:59:00.000Z",
    "PickupTimeSlot": "AM",
    "DaysToAdd": 0,
    "PickupTime": "1970-01-01T11:00:00.000Z"
  },
  {
    "WarehouseId": 2,
    "ShippingCarrierId": 2,
    "PostalCodeType": "ShipToCustomer",
    "TimeStart": "1970-01-01T00:00:00.000Z",
    "TimeEnd": "1970-01-01T15:59:00.000Z",
    "PickupTimeSlot": "PM",
    "DaysToAdd": 0,
    "PickupTime": "1970-01-01T17:00:00.000Z"
  },
  {
    "WarehouseId": 2,
    "ShippingCarrierId": 2,
    "PostalCodeType": "ShipToCustomer",
    "TimeStart": "1970-01-01T16:00:00.000Z",
    "TimeEnd": "1970-01-01T23:59:00.000Z",
    "PickupTimeSlot": "AM",
    "DaysToAdd": 1,
    "PickupTime": "1970-01-01T11:00:00.000Z"
  },
  {
    "WarehouseId": 1,
    "ShippingCarrierId": 3,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T00:00:00.000Z",
    "TimeEnd": "1970-01-01T15:59:00.000Z",
    "PickupTimeSlot": "PM",
    "DaysToAdd": 0,
    "PickupTime": "1970-01-01T17:00:00.000Z"
  },
  {
    "WarehouseId": 1,
    "ShippingCarrierId": 3,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T16:00:00.000Z",
    "TimeEnd": "1970-01-01T23:59:00.000Z",
    "PickupTimeSlot": "AM",
    "DaysToAdd": 1,
    "PickupTime": "1970-01-01T11:00:00.000Z"
  },
  {
    "WarehouseId": 2,
    "ShippingCarrierId": 4,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T00:00:00.000Z",
    "TimeEnd": "1970-01-01T15:59:00.000Z",
    "PickupTimeSlot": "PM",
    "DaysToAdd": 0,
    "PickupTime": "1970-01-01T17:00:00.000Z"
  },
  {
    "WarehouseId": 2,
    "ShippingCarrierId": 4,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T16:00:00.000Z",
    "TimeEnd": "1970-01-01T23:59:00.000Z",
    "PickupTimeSlot": "AM",
    "DaysToAdd": 1,
    "PickupTime": "1970-01-01T11:00:00.000Z"
  },
  {
    "WarehouseId": 1,
    "ShippingCarrierId": 5,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T00:00:00.000Z",
    "TimeEnd": "1970-01-01T16:22:00.000Z",
    "PickupTimeSlot": "PM",
    "DaysToAdd": 0,
    "PickupTime": "1970-01-01T17:00:00.000Z"
  },
  {
    "WarehouseId": 1,
    "ShippingCarrierId": 5,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T16:23:00.000Z",
    "TimeEnd": "1970-01-01T23:59:00.000Z",
    "PickupTimeSlot": "AM",
    "DaysToAdd": 1,
    "PickupTime": "1970-01-01T11:00:00.000Z"
  },
  {
    "WarehouseId": 2,
    "ShippingCarrierId": 6,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T00:00:00.000Z",
    "TimeEnd": "1970-01-01T15:59:00.000Z",
    "PickupTimeSlot": "PM",
    "DaysToAdd": 0,
    "PickupTime": "1970-01-01T17:00:00.000Z"
  },
  {
    "WarehouseId": 2,
    "ShippingCarrierId": 6,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T16:00:00.000Z",
    "TimeEnd": "1970-01-01T23:59:00.000Z",
    "PickupTimeSlot": "AM",
    "DaysToAdd": 1,
    "PickupTime": "1970-01-01T11:00:00.000Z"
  },
  {
    "WarehouseId": 1,
    "ShippingCarrierId": 1,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T06:00:00.000Z",
    "TimeEnd": "1970-01-01T15:59:00.000Z",
    "PickupTimeSlot": "PM",
    "DaysToAdd": 0,
    "PickupTime": "1970-01-01T17:00:00.000Z"
  },
  {
    "WarehouseId": 1,
    "ShippingCarrierId": 1,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T16:00:00.000Z",
    "TimeEnd": "1970-01-01T23:59:00.000Z",
    "PickupTimeSlot": "AM",
    "DaysToAdd": 1,
    "PickupTime": "1970-01-01T11:00:00.000Z"
  },
  {
    "WarehouseId": 1,
    "ShippingCarrierId": 1,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T00:00:00.000Z",
    "TimeEnd": "1970-01-01T05:59:00.000Z",
    "PickupTimeSlot": "AM",
    "DaysToAdd": 0,
    "PickupTime": "1970-01-01T11:00:00.000Z"
  },
  {
    "WarehouseId": 2,
    "ShippingCarrierId": 2,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T00:00:00.000Z",
    "TimeEnd": "1970-01-01T15:59:00.000Z",
    "PickupTimeSlot": "PM",
    "DaysToAdd": 0,
    "PickupTime": "1970-01-01T17:00:00.000Z"
  },
  {
    "WarehouseId": 2,
    "ShippingCarrierId": 2,
    "PostalCodeType": "ShipToDS",
    "TimeStart": "1970-01-01T16:00:00.000Z",
    "TimeEnd": "1970-01-01T23:59:00.000Z",
    "PickupTimeSlot": "AM",
    "DaysToAdd": 1,
    "PickupTime": "1970-01-01T11:00:00.000Z"
  }
]

我想按WarehouseIdShippingCarrierId和PostalCodeType对其进行分组。 每个objec都会有chlid对象数组,用于Timestart,TimeEnd等

我该如何实现

输出类似:

PickupTimeTable: [{
            WarehouseId: 1,
            ShippingCarrierId: 1,
            PostalCodeType: 'ShipToCustomer',
            PickupTimeSlots: [{
                    StartTime: '06:00:00.0000000',
                    EndTime: '15:59:00.0000000',
                    Slot: 'PM',
                    PickupTime: '17:00:00.0000000',
                    DaysToAdd: 0
                },
                {
                    StartTime: '16:00:00.0000000',
                    EndTime: '23:59:00.0000000',
                    Slot: 'AM',
                    PickupTime: '11:00:00.0000000',
                    DaysToAdd: 1
                },
                {
                    StartTime: '00:00:00.0000000',
                    EndTime: '05:59:00.0000000',
                    Slot: 'AM',
                    PickupTime: '11:00:00.0000000',
                    DaysToAdd: 0
                },
            ]
        }, ]

3 个答案:

答案 0 :(得分:1)

您可以使用reduce解决此问题

let groupBy = function(array, key) {
  return array.reduce(function(result, item) {
    (result[item[key]] = result[item[key]] || []).push(item);
    return result;
  }, {});
};

console.log(groupBy(yourarray, 'yourkey'));

答案 1 :(得分:0)

您可以使用lodash的groupBy功能-https://lodash.com/docs/4.17.11#groupBy

let groupBy = _.groupBy(array, 'WarehouseId');

给你->

wareHouseId1: [...], 
wareHouseId2: [...]

库不支持多级分组。

答案 2 :(得分:0)

您可以为由-分隔的那三个键的每个唯一组合创建一个具有新键的累加器对象。然后基于此新键,使用reduce合并数组。使用destructuringPickupTimeSlots中所需的键分隔为rest变量。然后使用Object.values获得最终数组:

const slots=[{"WarehouseId":1,"ShippingCarrierId":1,"PostalCodeType":"ShipToCustomer","TimeStart":"1970-01-01T06:00:00.000Z","TimeEnd":"1970-01-01T15:59:00.000Z","PickupTimeSlot":"PM","DaysToAdd":0,"PickupTime":"1970-01-01T17:00:00.000Z"},{"WarehouseId":1,"ShippingCarrierId":1,"PostalCodeType":"ShipToCustomer","TimeStart":"1970-01-01T16:00:00.000Z","TimeEnd":"1970-01-01T23:59:00.000Z","PickupTimeSlot":"AM","DaysToAdd":1,"PickupTime":"1970-01-01T11:00:00.000Z"},{"WarehouseId":1,"ShippingCarrierId":1,"PostalCodeType":"ShipToCustomer","TimeStart":"1970-01-01T00:00:00.000Z","TimeEnd":"1970-01-01T05:59:00.000Z","PickupTimeSlot":"AM","DaysToAdd":0,"PickupTime":"1970-01-01T11:00:00.000Z"},{"WarehouseId":2,"ShippingCarrierId":2,"PostalCodeType":"ShipToCustomer","TimeStart":"1970-01-01T00:00:00.000Z","TimeEnd":"1970-01-01T15:59:00.000Z","PickupTimeSlot":"PM","DaysToAdd":0,"PickupTime":"1970-01-01T17:00:00.000Z"},{"WarehouseId":2,"ShippingCarrierId":2,"PostalCodeType":"ShipToCustomer","TimeStart":"1970-01-01T16:00:00.000Z","TimeEnd":"1970-01-01T23:59:00.000Z","PickupTimeSlot":"AM","DaysToAdd":1,"PickupTime":"1970-01-01T11:00:00.000Z"},{"WarehouseId":1,"ShippingCarrierId":3,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T00:00:00.000Z","TimeEnd":"1970-01-01T15:59:00.000Z","PickupTimeSlot":"PM","DaysToAdd":0,"PickupTime":"1970-01-01T17:00:00.000Z"},{"WarehouseId":1,"ShippingCarrierId":3,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T16:00:00.000Z","TimeEnd":"1970-01-01T23:59:00.000Z","PickupTimeSlot":"AM","DaysToAdd":1,"PickupTime":"1970-01-01T11:00:00.000Z"},{"WarehouseId":2,"ShippingCarrierId":4,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T00:00:00.000Z","TimeEnd":"1970-01-01T15:59:00.000Z","PickupTimeSlot":"PM","DaysToAdd":0,"PickupTime":"1970-01-01T17:00:00.000Z"},{"WarehouseId":2,"ShippingCarrierId":4,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T16:00:00.000Z","TimeEnd":"1970-01-01T23:59:00.000Z","PickupTimeSlot":"AM","DaysToAdd":1,"PickupTime":"1970-01-01T11:00:00.000Z"},{"WarehouseId":1,"ShippingCarrierId":5,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T00:00:00.000Z","TimeEnd":"1970-01-01T16:22:00.000Z","PickupTimeSlot":"PM","DaysToAdd":0,"PickupTime":"1970-01-01T17:00:00.000Z"},{"WarehouseId":1,"ShippingCarrierId":5,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T16:23:00.000Z","TimeEnd":"1970-01-01T23:59:00.000Z","PickupTimeSlot":"AM","DaysToAdd":1,"PickupTime":"1970-01-01T11:00:00.000Z"},{"WarehouseId":2,"ShippingCarrierId":6,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T00:00:00.000Z","TimeEnd":"1970-01-01T15:59:00.000Z","PickupTimeSlot":"PM","DaysToAdd":0,"PickupTime":"1970-01-01T17:00:00.000Z"},{"WarehouseId":2,"ShippingCarrierId":6,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T16:00:00.000Z","TimeEnd":"1970-01-01T23:59:00.000Z","PickupTimeSlot":"AM","DaysToAdd":1,"PickupTime":"1970-01-01T11:00:00.000Z"},{"WarehouseId":1,"ShippingCarrierId":1,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T06:00:00.000Z","TimeEnd":"1970-01-01T15:59:00.000Z","PickupTimeSlot":"PM","DaysToAdd":0,"PickupTime":"1970-01-01T17:00:00.000Z"},{"WarehouseId":1,"ShippingCarrierId":1,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T16:00:00.000Z","TimeEnd":"1970-01-01T23:59:00.000Z","PickupTimeSlot":"AM","DaysToAdd":1,"PickupTime":"1970-01-01T11:00:00.000Z"},{"WarehouseId":1,"ShippingCarrierId":1,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T00:00:00.000Z","TimeEnd":"1970-01-01T05:59:00.000Z","PickupTimeSlot":"AM","DaysToAdd":0,"PickupTime":"1970-01-01T11:00:00.000Z"},{"WarehouseId":2,"ShippingCarrierId":2,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T00:00:00.000Z","TimeEnd":"1970-01-01T15:59:00.000Z","PickupTimeSlot":"PM","DaysToAdd":0,"PickupTime":"1970-01-01T17:00:00.000Z"},{"WarehouseId":2,"ShippingCarrierId":2,"PostalCodeType":"ShipToDS","TimeStart":"1970-01-01T16:00:00.000Z","TimeEnd":"1970-01-01T23:59:00.000Z","PickupTimeSlot":"AM","DaysToAdd":1,"PickupTime":"1970-01-01T11:00:00.000Z"}]

const merged = slots.reduce((r, { WarehouseId, ShippingCarrierId, PostalCodeType,...rest }) => {
  const key = `${WarehouseId}-${ShippingCarrierId}-${PostalCodeType}`;
  r[key] = r[key] || { WarehouseId, ShippingCarrierId, PostalCodeType, PickupTimeSlots: [] };
  r[key]["PickupTimeSlots"].push(rest)
  return r;
}, {})

const timeTable = Object.values(merged)
console.log(timeTable)