How do I reduce the excessive amount of "ifs"?

public void OnDrop(PointerEventData eventData)
{
GameObject dropped = eventData.pointerDrag;
InventoryItem inventoryItem = dropped.GetComponent();

            InventorySlot slot1 = inventoryManager.inventorySlot1Object;
            InventorySlot slot2 = inventoryManager.inventorySlot2Object;

            InventoryItem itemInSlot1 = slot1.GetComponentInChildren<InventoryItem>();
            InventoryItem itemInSlot2 = slot2.GetComponentInChildren<InventoryItem>();

            //Item Slot 1
            if (transform.childCount > 0 && itemInSlot1.item == inventoryItem.item)
            {
                itemInSlot1.count++;
            }
            //Item Slot 2
            if (transform.childCount > 0 && itemInSlot2.item == inventoryItem.item)
            {
                itemInSlot2.count++;
            }
        }

My intention is to repeat this many times, but I wanted to know if there is any way to reduce this. Because they do the same thing:

//Item Slot 1
            if (transform.childCount > 0 && itemInSlot1.item == inventoryItem.item)
            {
                itemInSlot1.count++;
            }
            //Item Slot 2
            if (transform.childCount > 0 && itemInSlot2.item == inventoryItem.item)
            {
                itemInSlot2.count++;
            }
        }

In general you will want to have something that uses a combination of loops and lists/arrays.

So instead of creating one variable for each itemslot, have an array of objects:

 List<InventorySlot> slots  = new List<InventorySlot>();

 List<InventoryItem> itemsInSlots = new List<InventoryItem>();

Then your code simplifies:

         public void OnDrop(PointerEventData eventData)
         {
             GameObject dropped = eventData.pointerDrag;
             InventoryItem inventoryItem = dropped.GetComponent<InventoryItem>();

             if (transform.childCount < 0)
             {
                    return;
             }
             foreach(var slot in itemsInSlots){
                  if(slot.item == inventoryItem.item){
                           slot.count++;
                           break;
                  }
             }
             if (transform.childCount > 0 && itemInSlot2.item == inventoryItem.item)
             {
                 itemInSlot2.count++;
             }
         }

All initialization like this:

             InventorySlot slot1 = inventoryManager.inventorySlot1Object;
             InventorySlot slot2 = inventoryManager.inventorySlot2Object;
 
             InventoryItem itemInSlot1 = slot1.GetComponentInChildren<InventoryItem>();
             InventoryItem itemInSlot2 = slot2.GetComponentInChildren<InventoryItem>();

should be done in the Start function.

try to give your inventoryManager a list as well.

Then you can initialize like this:

   foreach(var slot in inventoryManager.itemsInSlots){
          itemsInSlots.add(slot.GetComponentInChildren<InventoryItem>();
   }