对象数组中的组属性-Javascript

时间:2019-07-17 11:45:28

标签: javascript arrays javascript-objects

更新:

我可以通过以下方式获取ID和必填项的内容:

let group_to_values = salesRes.reduce((obj,elem)=> {让subprops = {item:elem.item,itemQuantity:elem.itemQuantity,itemPrice:elem.itemPrice}; obj [elem.id] = obj [ elem.id] || []; obj [elem.id] .push(subprops); return obj;},{});让输出= Object.keys(group_to_values).map(key  => {return {id:key,items:group_to_values [key]}; });

如何在输出中包括其余其他道具,例如status,totalAmount等?

原始帖子:

我正在得到这个对象数组:

[{
    id: 1,
    status: "completed",
    totalAmount: 3100,
    date: "2019-06-29T14:27:55.000Z",
    email: "test@test.com",
    customerNote: "test note",
    phone: "300-0000000",
    item: "item1",
    itemQuantity: 3,
    itemPrice: 1000
}, {
    id: 1,
    status: "completed",
    totalAmount: 3100,
    date: "2019-06-29T14:27:55.000Z",
    email: "test@test.com",
    customerNote: "test note",
    phone: "300-0000000",
    item: "item2",
    itemQuantity: 1,
    itemPrice: 100
}, {
    id: 3,
    status: "completed",
    totalAmount: 4500,
    date: "2019-07-16T15:19:59.000Z",
    email: "test2@tes.com",
    customerNote: "Please deliver asap",
    phone: "00000000000",
    item: "item3",
    itemQuantity: 1,
    itemPrice: 2000
}, {
    id: 3,
    status: "completed",
    totalAmount: 4500,
    date: "2019-07-16T15:19:59.000Z",
    email: "test2@tes.com",
    customerNote: "Please deliver asap",
    phone: "00000000000",
    item: "item4",
    itemQuantity: 1,
    itemPrice: 2500
}, {
    id: 2,
    status: "completed",
    totalAmount: 140,
    date: "2019-06-29T15:22:03.000Z",
    email: "test@test.com",
    customerNote: "note",
    phone: "300-0000000",
    item: "item1",
    itemQuantity: 1,
    itemPrice: 50
}, {
    id: 2,
    status: "completed",
    totalAmount: 140,
    date: "2019-06-29T15:22:03.000Z",
    email: "test@test.com",
    customerNote: "note",
    phone: "300-0000000",
    item: "item3",
    itemQuantity: 1,
    itemPrice: 90
}]

我正在尝试将项目(item,itemQuantity和itemPrice)分组到子数组中。我该如何转换:

 [{
    id: 1,
    status: "completed",
    totalAmount: 3100,
    date: "2019-06-29T14:27:55.000Z",
    email: "test@test.com",
    customerNote: "test note",
    phone: "300-0000000",
    items: [{
        item: "item1",
        itemQuantity: 3,
        itemPrice: 1000
    }, {
        item: "item2",
        itemQuantity: 1,
        itemPrice: 100
    }]
}, {
    id: 3,
    status: "completed",
    totalAmount: 4500,
    date: "2019-07-16T15:19:59.000Z",
    email: "test2@tes.com",
    customerNote: "Please deliver asap",
    phone: "00000000000",
    items: [{
        item: "item3",
        itemQuantity: 1,
        itemPrice: 2000
    }, {
        item: "item4",
        itemQuantity: 1,
        itemPrice: 2500
    }]
}, {
    id: 2,
    status: "completed",
    totalAmount: 140,
    date: "2019-06-29T15:22:03.000Z",
    email: "test@test.com",
    customerNote: "note",
    phone: "300-0000000",
    items: [{
        item: "item1",
        itemQuantity: 1,
        itemPrice: 50
    }, {
        item: "item3",
        itemQuantity: 1,
        itemPrice: 90
    }]
}];

0 个答案:

没有答案