编辑代码

const boxes = [{id:1},{id:2},{id:1},{id:1},{id:3},{id:2},{id:2}]
// const res = [
//     {id:1,qty:2},
//     {id:2,qty:1},
// ]

// const boxs_res = boxes.reduce((acc: any, cur) => {
//     if (cur.packaging_box_id) {
//         const existingBox = acc.find((item: any) => item.id === cur.packaging_box_id)
//         if (existingBox) {
//             existingBox.quantity++
//         } else {
//             acc.push({
//                 id: cur.packaging_box_id,
//                 name: packagingBoxList.value.find((box: any) => box.id === cur.packaging_box_id).box_name,
//                 price: packagingBoxList.value.find((box: any) => box.id === cur.packaging_box_id).price,
//                 quantity: 1,
//             })
//         }
//     }
//     return acc
// }, [])
const result = boxes.reduce((acc, curr) => {
    const existingItem = acc.find(item => item.id === curr.id);
    if (existingItem) {
        existingItem.qty++;
    } else {
        acc.push({ id: curr.id, qty: 1 });
    }
    return acc;
}, []);

console.log(result);