-2

I have an array which has keys eventId and selectedNumber. In the array same eventid can be present in multiple objects but selectedNumber value will be always different. My aim is to make a nested array in which each object will have unique eventId But selectedNumber will become an array having numbers from each of those objects having the same eventId. I tried using lodash _.groupBy() method but its just combines the objects into array and add it to the value with key as eventId. I don't want that. Anyway to do it?

Input:--

[{
  "eventId" : "636939dde9341f2fbbc7256e",
  "selectedNumber" : "20"
},
{
  "eventId" : "636939dde9341f2fbbc7256e",
  "selectedNumber" : "30"
},
{
  "eventId" : "63693a55e9341f2fbbc725c0",
  "selectedNumber" : "50"
}]

Result:--


[{
  "eventId" : "636939dde9341f2fbbc7256e",
  "selectedNumber" : ["20", "30"]
},
{
  "eventId" : "63693a55e9341f2fbbc725c0",
  "selectedNumber" : "50"
}]

Somnath Pal
  • 190
  • 1
  • 15
  • 1
    Easy, create a result array, loop through the existing array, check if the id already exists in the result array, if it does add the number to the selectedNumber array of that id, if not add the object to the result array. – Geshode Nov 10 '22 at 05:07
  • can u show me some sample code? – Somnath Pal Nov 10 '22 at 05:07
  • Its easy to give negative vote and logic. Not that easy to write a code. Anyways, my code is done. – Somnath Pal Nov 10 '22 at 05:18
  • 1
    Stackoverflow is not a code writing site. It is here to help people with specific problems/questions. See it as a learning facility. Giving you the right steps to achieve your goal and letting you try it out yourself is way better teaching than just giving you a complete code, for which you didn't need to think at all. I guess, the negative votes are because, your question lacks some points mentioned in: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – Geshode Nov 10 '22 at 05:23

2 Answers2

0

let newarr = [] 
oldArr.map((x,i)=>{ 
  if(i==0){ 
    const numArr = [] 
    numArr.push(x.selectedNumber) 
    delete x.selectedNumber 
    x.numArr = numArr newarr.push(x) 
  }else{ 
    if(oldArr[i].eventId == oldArr[i-1].eventId){
      const temp = x.selectedNumber 
      delete x.selectedNumber
      newarr[i-1].numArr.push(temp) 
    }else{ 
      const numArr = [] 
      numArr.push(x.selectedNumber) 
      delete x.selectedNumber 
      x.numArr = numArr 
      newarr.push(x) 
    } 
  } 
})
Somnath Pal
  • 190
  • 1
  • 15
  • 1
    Why would you use `map()` if you're just going to discard the return value? Why not just use `forEach()` instead? – Robby Cornelissen Nov 10 '22 at 05:25
  • Actually I needed 2 keys for the inner array and apart from eventId and selectedNumber, I have other keys also which needed to merged. Yes, I can certainly use forEach also. – Somnath Pal Nov 10 '22 at 08:06
0

Just reduce your input to an object, and map the object entries to the desired array format:

const input = [{
  "eventId" : "636939dde9341f2fbbc7256e",
  "selectedNumber" : "20"
},
{
  "eventId" : "636939dde9341f2fbbc7256e",
  "selectedNumber" : "30"
},
{
  "eventId" : "63693a55e9341f2fbbc725c0",
  "selectedNumber" : "50"
}];

const result = Object.entries(input.reduce((a, {eventId, selectedNumber}) => {
  a[eventId] = a[eventId] || [];
  a[eventId].push(selectedNumber)
  return a;
}, {})).map(([eventId, selectedNumber]) => ({ eventId, selectedNumber }));

console.log(result);

Instead of creating the intermediate lookup object, you could directly reduce to an array, but it will have a negative impact on the solution's time complexity.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156