Find the closest matching number

Today I needed to find a way to match a number in an array to a specific number. For my case, it was matching the window width with each of the Bootstrap 4 breakpoints.

I found a version of this snippet somewhere (I am assuming Stack Overflow, but I cannot find it!), and thought it was a gem worth holding onto because of its simplicity and elegance.