How can I determine which array index my number is closest to? [duplicate]
问题 This question already has answers here : get closest number out of array (17 answers) Closed 5 years ago . I have an array and var that look like this: var paymentMilestones = [0, 30, 60, 90]; var daysOverdue = 33; In JavaScript, how can I programmatically determine which of the paymentMilestones indexes the daysOverdue var is closest to? I need that value returned, so in this case the function should return 30 . 回答1: A quick answer (if there is a tie it will bring back both - assuming the