How do i find nearest index

what is the best way to find the nearest index

print(functionthatineedhelpmaking(5,{"boi",nil,nil,nil,nil,"shouldprintthis"}))

so basically it should print the element with the index closest to the first parameter how do i make such a function it is very important for my scripts