'How to stack items held in a map of items and their slot numbers in an inventory?

I have a map that holds items and their slot numbers in an inventory consisting of 28 slots. The map is on the client side. The server holds an array containing the ID and quantity of each item.

Here is what it looks like on the server:

const items = [
      { id: 1, quantity: 10},
      { id: 2, quantity: 30 },
      { id: 3, quantity: 5 },
      { id: 4, quantity: 7 },
    ];

Here is the map that I have on the client:

items: Map<number, Item> = new Map();

Here is the structure of the Item data type:

type Item = {
id: number,
name: string,
category: string,
}

What is the best way to update the items map on the client so that it also keeps track of the quantity? I don't want to add a quantity field on the Item type.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source