Try randomly checking "will this item drop?" for every item. You could store the information for item and drop chance as pairs in a list:
drops = [["sword",5],["hp_potion",35],["mp_potion",35]]
loot = []
for i in drops:
if random_chance(i[1]):
loot.append(i[2])
give_player_loot()
...but this does not account for quantity, and maybe you'd like to prevent one item from dropping if another does. My solution for this would be nesting lists even further, with an item name, chance, and min/max quantity going in list A. List A is put within pool B, within the drops variable, C.
So, this would look something like:
# SLOW BUT EASY WAY TO BUILD LOOT TABLE
# You do this when the monster is created, if you are keeping a list of monsters
# that is made at the start of the game, this is also where you'd put this.
# this example loot pool has a 35% chance to drop 1-3 HP potions, OR, failing
# that, a 35% chance to drop 1-2 MP potions.
A = ["hp_potion",35,1,3] # individual item drop chance
A2 = ["mp_potion",35,1,2] # another one, to help describe the concept.
B = [A,A2] # the loot, A and A2 is going into pool B.
C = [B] # B goes into the final drop pool
monster_drops = C
# COMPACT WAY:
monster_drops = [[["hp_potion",35,1,3],["mp_potion",35,1,2]]]
Next, we have to iterate between all drops like so:
loot = []
for i in monster_drops: # go through all pools
for j in i: # go through all items
if random_chance(j[1]):
loot += [j[0]]*random_range(j[2],j[3]) # add this item to the loot,
# multiplied by the number of
# items it should drop.
break # exit the loop
give_player_loot(loot)
Sorry for the poor formatting, it's based on python and I don't really know how to use StackExchange, but I hope this was helpful!
(Also, I noticed you are using JS, but hopefully the same concepts apply there!)