根据reactjs中的分组条件减少数组

Reduce array according to grouping condition in reactjs

我有数组:

array = [
    {
        id: 1,
        count: 0.5
        cost: 100
        user: {id: 1, name: "John 1"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    },
    {
        id: 2,
        count: 2.5
        cost: 200
        user: {id: 1, name: "John 1"},
        type: {id: 2, name: "T2"},
        period: {id: 2, name: "2022"}
    },
    {
        id: 3,
        count: 2.5
        cost: 400
        user: {id: 1, name: "John 1"},
        type: {id: 2, name: "T2"},
        period: {id: 2, name: "2022"}
    },
    {
        id: 4,
        count: 1.5
        cost: 100
        user: {id: 2, name: "John 2"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    },
    {
        id: 5,
        count: 0.5
        cost: 500
        user: {id: 3, name: "John 3"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    }
]

我需要用两种不同的方式减少。

首先:按userperiodtype分组,其中cost是根据cost匹配的那些的总和之前的条件。

array1 = [
    {
        id: 1,
        count: 0.5
        cost: 100
        user: {id: 1, name: "John 1"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    },
    {
        id: 2,
        count: 2.5
        cost: 600
        user: {id: 1, name: "John 1"},
        type: {id: 2, name: "T2"},
        period: {id: 2, name: "2022"}
    },
    {
        id: 3,
        count: 1.5
        cost: 100
        user: {id: 2, name: "John 2"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    },
    {
        id: 4,
        count: 0.5
        cost: 500
        user: {id: 3, name: "John 3"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    }
]

第二:以相同的方式分组,但 cost 是包含匹配的 cost 的元素。

array2 = [
    {
        id: 1,
        count: 0.5
        cost: 100
        user: {id: 1, name: "John 1"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    },
    {
        id: 2,
        count: 2.5
        cost: [{200},{400}]
        user: {id: 1, name: "John 1"},
        type: {id: 2, name: "T2"},
        period: {id: 2, name: "2022"}
    },
    {
        id: 3,
        count: 1.5
        cost: 100
        user: {id: 2, name: "John 2"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    },
    {
        id: 4,
        count: 0.5
        cost: 500
        user: {id: 3, name: "John 3"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    }
]

注意:对于相同的 usertypeperiod,它将始终是相同的帐户

我该怎么做?谢谢!

更新:我试过了

const array1 = array((acc, elem) => {
      if (acc.some((accElem) => accElem.user.id === elem.user.id && accElem.period.id === elem.period.id && accElem.type.id === elem.type.id)) {
        elem.cost = array
          .filter((e) => e.user.id === elem.user.id && e.period.id === elem.period.id && e.type.id === elem.type.id)
          .reduce((acc, e) => acc + e.cost, 0);
        ...
      }
      
      if (acc.some((accElem) => accElem.user.id === elem.user.id && accElem.period.id === elem.period.id && accElem.type.id !== elem.type.id)) {
          elem.cost = array
            .filter((e) => e.user.id === elem.user.id && e.period.id === elem.period.id && e.type.id === elem.type.id)
            .reduce((acc, e) => e.cost, 0);
         ...
        }

      return acc.concat(elem);
    }, []);

const input =  [
    {
        id: 1,
        count: 0.5,
        cost: 100,
        user: {id: 1, name: "John 1"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    },
    {
        id: 2,
        count: 2.5,
        cost: 200,
        user: {id: 1, name: "John 1"},
        type: {id: 2, name: "T2"},
        period: {id: 2, name: "2022"}
    },
    {
        id: 3,
        count: 2.5,
        cost: 400,
        user: {id: 1, name: "John 1"},
        type: {id: 2, name: "T2"},
        period: {id: 2, name: "2022"}
    },
    {
        id: 4,
        count: 1.5,
        cost: 100,
        user: {id: 2, name: "John 2"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    },
    {
        id: 5,
        count: 0.5,
        cost: 500,
        user: {id: 3, name: "John 3"},
        type: {id: 1, name: "T1"},
        period: {id: 1, name: "2021"}
    }
]

const firstGroup = JSON.parse(JSON.stringify(input)).reduce((groupedData, user) => {
      const matchingUserIndex = groupedData.length > 0 && groupedData.findIndex(groupedUser => groupedUser.user.id === user.user.id && groupedUser.type.id === user.type.id && groupedUser.period.id === user.period.id);
      if(matchingUserIndex && matchingUserIndex !== -1){
        groupedData[matchingUserIndex].cost += user.cost
      } else {
        groupedData.push(user);
      } 
  
  return groupedData
  
}, [])

console.log('First Group ********** \n', firstGroup)


const secondGroup = JSON.parse(JSON.stringify(input)).reduce((groupedData, user) => {
      const matchingUserIndex = groupedData.length > 0 && groupedData.findIndex(groupedUser => groupedUser.user.id === user.user.id && groupedUser.type.id === user.type.id && groupedUser.period.id === user.period.id);
      if(matchingUserIndex && matchingUserIndex !== -1){
        if(Array.isArray(groupedData[matchingUserIndex].cost)){
          groupedData[matchingUserIndex].cost.push(user.cost)
        } else {
           groupedData[matchingUserIndex].cost = [groupedData[matchingUserIndex].cost, user.cost]
        }
      } else {
        groupedData.push(user);
      } 
  
  return groupedData
  
}, [])

console.log('second Group ********** \n', secondGroup)