I'm pretty sure the keys in the slots table matches the slotIndices. So you could loop through the slots and check to see if each key exists in the table.
Lua Code:
local inventory = PLAYER_INVENTORY.inventories[INVENTORY_BACKPACK].slots
local bagSize = GetBagSize(BAG_BACKPACK)
for i = 0, bagSize-1 do
if not inventory[i] then
-- do whatever inventory[i] is an empty slot
end
end
Or you could make it into a function and each time you find an empty slot throw it into a table, then return the table & you'll have all of the empty slots to do whatever you want with them.
Although merlights answer is probably better for what your doing, just giving some options.