编辑代码

const box_data = {
    "packaging_box_list": [
        {
            "id": 1,
            "region": "zh-cn",
            "box_type": 2,
            "box_name": "境内直运通用包装箱",
            "length": "1.00",
            "width": "1.00",
            "height": "1.00",
            "sort": 0,
            "weigh": 10,
            "price": "2.00",
            "is_offer_by_dsg": 1,
            "create_time": 0,
            "update_time": 0
        },
        {
            "id": 10,
            "region": "zh-cn",
            "box_type": 2,
            "box_name": "境内直运通用包装箱",
            "length": "1.00",
            "width": "1.00",
            "height": "1.00",
            "sort": 0,
            "weigh": 10,
            "price": "5.00",
            "is_offer_by_dsg": 1,
            "create_time": 0,
            "update_time": 0
        }
    ]
};

const packagingBoxList = {
    value: [
        {
            "packaging_box_id": 10,
            "box_insurance_price_id": 0,
            "laterEnter": false,
            "goods": [
                {
                    "id": 1,
                    "name": ""
                }
            ],
            "length": "1.00",
            "width": "1.00",
            "height": "1.00"
        },
        {
            "packaging_box_id": 10,
            "box_insurance_price_id": 0,
            "laterEnter": false,
            "goods": [
                {
                    "id": 2,
                    "name": ""
                }
            ],
            "length": "1.00",
            "width": "1.00",
            "height": "1.00"
        },
        {
            "packaging_box_id": 1,
            "box_insurance_price_id": 0,
            "laterEnter": false,
            "goods": [
                {
                    "id": 1,
                    "name": ""
                }
            ],
            "length": "1.00",
            "width": "1.00",
            "height": "1.00"
        },
        {
            "packaging_box_id": 1,
            "box_insurance_price_id": 0,
            "laterEnter": false,
            "goods": [
                {
                    "id": 1,
                    "name": ""
                }
            ],
            "length": "1.00",
            "width": "1.00",
            "height": "1.00"
        }
    ]
};

// 使用reduce方法整理出新的数组boxs_res
const boxs_res = packagingBoxList.value.reduce((acc, cur) => {
    const existingBox = acc.find(item => item.id === cur.packaging_box_id);
    if (existingBox) {
        existingBox.count++;
    } else {
        acc.push({ id: cur.packaging_box_id, price: box_data.packaging_box_list.find(box => box.id === cur.packaging_box_id).price, count: 1 });
    }
    return acc;
}, []);

console.log(boxs_res);