0

I have an array with objects that have an object property called "operationGroup" with the "groupId" property, like this:

[{
    operation: 11111,
    operationGroup: null
  },
  {
    operation: 22222,
    operationGroup: {
      groupId: 20
    }
  },
  {
    operation: 33333,
    operationGroup: {
      groupId: 1
    }
  },
  {
    operation: 44444,
    operationGroup: {
      groupId: 20
    }
  }
]

How can I find all the objects with the same groupId and add them to a new array property (groupedOperations) in each object with that groupId? It should not add the array property if the operationGroup is null or if only one groupId is found. The expected output is this:

[{
    operation: 11111,
    operationGroup: null
  },
  {
    operation: 22222,
    operationGroup: {
      groupId: 20
    },
    groupedOperations: [{
      operation: 22222,
      operationGroup: {
        groupId: 20
      },
      {
        operation: 44444,
        operationGroup: {
          groupId: 20
        }
      }
    }]
  },
  {
    operation: 33333,
    operationGroup: {
      groupId: 1
    }
  },
  {
    operation: 44444,
    operationGroup: {
      groupId: 20
    },
    groupedOperations: [{
        operation: 44444,
        operationGroup: {
          groupId: 20
        }
      },
      {
        operation: 22222,
        operationGroup: {
          groupId: 20
        },
      }
    ]
  }
]
4
  • why don't you have "groupedOperations" key in your expected output when "groupId" : 1 ? . Is it on purpose or just a mistake Commented Mar 15, 2021 at 21:32
  • if the array has only one object with the same groupId then the property groupedOperations should not be added Commented Mar 15, 2021 at 21:41
  • 2
    Please edit your question to show any research you've done and any attempts you've made based on that research. For instance, there are many questions about grouping arrays, including Most efficient method to groupby on an array of objects or Group array items using object Commented Mar 15, 2021 at 21:53
  • Try to solve it yourself. Look into array.reduce or array.forEach. You can ask more specific questions on Stackoverflow, but it's not a forum for solving problems for you. Commented Mar 15, 2021 at 21:56

2 Answers 2

1

let reqArray =[{
    operation: 11111,
    operationGroup: null
  },
  {
    operation: 22222,
    operationGroup: {
      groupId: 20
    }
  },
  {
    operation: 33333,
    operationGroup: {
      groupId: 1
    }
  },
  {
    operation: 44444,
    operationGroup: {
      groupId: 20
    }
  }
]
let groups = {}
let groupReq = []
for (let req of reqArray) {
    if (!req.operationGroup) continue;
    if (!groups[req.operationGroup.groupId]) groups[req.operationGroup.groupId] = [];
    groups[req.operationGroup.groupId].push(req)
}

for(let req of reqArray){
    if(req.operationGroup && groups[req.operationGroup.groupId].length >= 2 ){
        req.groupedOperations = groups[req.operationGroup.groupId]
    }
    groupReq.push(req)
}
console.log(groupReq,groups)

first Filter and group all operation based on groupId. then loop over request data again to update groupedOperations property

Sign up to request clarification or add additional context in comments.

Comments

1
var list = [{
    operation: 11111,
    operationGroup: null
  },
  {
    operation: 22222,
    operationGroup: {
      groupId: 20
    }
  },
  {
    operation: 33333,
    operationGroup: {
      groupId: 1
    }
  },
  {
    operation: 44444,
    operationGroup: {
      groupId: 20
    }
  }
];

var groupedById =  list.reduce((acc, x) =>  { 
    if(x.operationGroup != null) {
        let groupId = x.operationGroup.groupId; 
        if(!acc[groupId]){
        acc[groupId] = [];
        }
        acc[groupId].push(x); 
    }
    return acc;
}, {});



list.map(x => {
    if(x.operationGroup != null) {
        let groupId = x.operationGroup.groupId;
        if(groupedById[groupId] && groupedById[groupId].length > 1){            
             x["groupedOperations"] = groupedById[groupId];          
        }
    }
});

console.log(list);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.