gpt4 book ai didi

javascript - 如何根据Javascript中的多个键对对象数组进行分组?

转载 作者:行者123 更新时间:2023-12-04 00:28:09 28 4
gpt4 key购买 nike

我有如下数组

 [
{
"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"
}
]

我想按 WarehouseId 分组, ShippingCarrierId和邮政编码类型。
每个对象都有 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 个键的每个唯一组合创建一个带有新键的累加器对象。 .然后基于这个新键使用 reduce 合并数组.使用 destructuringPickupTimeSlots 中分隔您需要的 key 到 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)

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com