我如何使用 reduce 来计算总数并修改我的数组?

How do I use reduce to count up totals and modify my array?

我使用 Redux 将我所有的产品存储在一个列表中。显示的列表如下所示:

[
  {
    "id": 1,
    "name": "BLACK TEA",
    "supplieruuid": "SLIGRO",
    "price": 1.10,
  },
  {
    "id": 2,
    "name": "GREEN TEA",
    "supplieruuid": "SLIGRO",
    "price": 1.10,
  },
  {
    "id": 3,
    "name": "PURPLE TEA",
    "supplieruuid": "BUNZL",
    "price": 1.10,
  },
  {
    "id": 4,
    "name": "RAINBOW TEA",
    "supplieruuid": "BUNZL",
    "price": 1.10,
  },
] 

我正在使用这个 reduce 函数通过键 supplieruuid.

将这些产品分组在一起
    const sortedBySupplierUUID = state.entities.cart.list.reduce(
    (hash, { ["supplieruuid"]: value, ...rest }) => ({ ...hash, [value]: (hash[value] || []).concat({ ["supplieruuid"]: value, ...rest }) }),
    {}
  );
  return Object.keys(sortedBySupplierUUID).map((key) => ({ title: key, data: sortedBySupplierUUID[key] }));

这个returns的内容如下:

 [
    {
        title: "SLIGRO",
        data: [
        {
            "id": 1,
            "name": "BLACK TEA",
            "price": 1.10,
        },
        {
            "id": 2,
            "name": "GREEN TEA",
            "price": 1.10,
        },
        ],
    },
    {
        title: "BUNZL",
        data: [
        {
            "id": 3,
            "name": "PURPLE TEA",
            "price": 1.10,
        },
        {
            "id": 4,
            "name": "RAINBOW TEA",
            "price": 1.10,
        },
        ],
    },
] 

这一切都很好,除了我将 total 添加到对象,这将计算每个对象的“数据”数组中项目的总价格作为以下:

[
        {
            title: "SLIGRO",
            total: 2.20,
            data: [
            {
                "id": 1,
                "name": "BLACK TEA",
                "price": 1.10,
            },
            {
                "id": 2,
                "name": "GREEN TEA",
                "price": 1.10,
            },

            ],
        },
        {
            title: "BUNZL",
            total: 2.20,
            data: [
            {
                "id": 3,
                "name": "PURPLE TEA",
                "price": 1.10,
            },
            {
                "id": 4,
                "name": "RAINBOW TEA",
                "price": 1.10,
            },
            ],
        },
    ] 

如何通过修改我使用的 reduce 函数来实现这一点?

const selectSortedItems = (state) => {
  const sortedBySupplierUUID = state.entities.cart.list.reduce((hash, { ["supplieruuid"]: value, ...rest }) => ({ ...hash, [value]: (hash[value] || []).concat({ ...rest }) }), {});
  return Object.keys(selectSortedItems(sortedBySupplierUUID)).map(key => (
         { 
             title: key, 
             total: sortedBySupplierUUID[key].reduce((acc, cur) => acc + cur.price),
             data: sortedBySupplierUUID[key] 
         })
  )};
}

更直观的做法

const selectSortedItems = (state) => {
    const itemsMap = {};

    state.entities.cart.list.map((item) => {
        if (itemsMap[item.supplieruuid]) {
            itemsMap[item.supplieruuid].total += item.price;
            itemsMap[item.supplieruuid].data.push(item);
        } else {
            itemsMap[item.supplieruuid] = {
                title: item.supplieruuid,
                total: item.price,
                data: [item]
            }
        }
    });

    return Object.values(itemsMap);
}