I have an array orders that are objects of orders made. I want to make a report out of this and create a way to remove any duplicates based on id but would like to keep the data of bookId and count in a new array. I also am attempting to add the counts together if multiple counts exist for the same id.
I am having issues achieving this and not sure which direction to take.
- First I have - orderListwhich is an array of all bookIds, count and name
- Second I tried using new Set to remove any duplicates from - orders
- Third I am try add - ordersif the the userIds from- orderListand- ordersare a match.
Here is an example of my snippet:
    const orders = [
        {
            "userId": 1,
            "name": "Person 1",
            "status": "active",
            "bookId": 24,
            "count": 1
        }, 
            {
            "userId": 1,
            "name": "Person 1",
            "status": "active",
            "bookId": 25,
            "count": 2
        }, 
        {
            "userId": 2,
            "name": "Person 2",
            "status": "active",
            "bookId": 8,
            "count": 2
        }, {
            "userId": 1,
            "name": "Person 1",
            "status": "active",
            "bookId": 24,
            "count": 3
        }, {
            "userId": 3,
            "name": "Person 3",
            "status": "active",
            "bookId": 99,
            "count": 1
        }
    ]
    
  
const orderList = orders.map(item => ({count: item.count, id: item.bookId,  userId: item.userId}))
const uniqueOrder = [...new Set(orders)]
let results = orderList.map(item => ({ ...item,
  orders: uniqueOrder.filter(f => f.userId == item.userId)
}));
console.log(results)Here is an example of the orders array:
const orders = [
    {
        "userId": 1,
        "name": "Person 1",
        "status": "active",
        "bookId": 24,
        "count": 1
    }, 
        {
        "userId": 1,
        "name": "Person 1",
        "status": "active",
        "bookId": 25,
        "count": 2
    }, 
    {
        "userId": 2,
        "name": "Person 2",
        "status": "active",
        "bookId": 8,
        "count": 2
    }, {
        "userId": 1,
        "name": "Person 1",
        "status": "active",
        "bookId": 24,
        "count": 3
    }, {
        "userId": 3,
        "name": "Person 3",
        "status": "active",
        "bookId": 99,
        "count": 1
    }
]
console.log(orders)
I am expecting an outcome that looks like:
const results = [
  {
    userId: 1,
    name: 'Person 1',
    status: 'active',
    orders: [
      {
        bookId: 24,
        count: 4,
      },
      {
        bookId: 25,
        count: 2,
      },
    ],
  },
  {
    userId: 2,
    name: 'Person 2',
    status: 'active',
    orders: [
      {
        bookId: 25,
        count: 2,
      },
    ],
  },
  {
    userId: 3,
    name: 'Person 3',
    status: 'active',
    orders: [
      {
        bookId: 99,
        count: 1,
      },
    ],
  },
];
console.log(results); 
     
     
     
    