javascript find nearest element

// Find the number from `arr` which is closest to `n`
const closest = (arr, n) => arr.reduce((prev, curr) => Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev);

// Or
const closest = (arr, n) => arr.sort((a, b) => Math.abs(a - n) - Math.abs(b - n))[0];

// Example
closest([29, 87, 8, 78, 97, 20, 75, 33, 24, 17], 50);   // 33

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
javascript get nearest integer javascript nearest full number find nearest value in array javascript find nearest in array javascript get nearest element sibling javascript number nearest in javascript nearest element javascript javascript nearest sibling get nearest value from javascript array\ to nearest whole number javascript javascript nearest whole number nearest whole number javascript nearest integer javascript find the nearest element in array js nearest whole number how to find nearest number in javascript find nearest element in array get nearest sibling javascript javascript nearest get nearest element javascript nearest a tag nearest value javascript find nearest value javascript nearest value from an array js nearest element in an array javascript get nearest element how to get the nearest html js how to find the nearest value in javascript js get nearest element javascript array find nearest number nearest javascript search the nearest element in array javascript javascript find the closest element with class click event closest sibling react element.closest js find closet javascript clasest moziila htnl el closet e.target.closet div with closeset parent with class element in javascript javascript get the closest web component closet in dom js closest .this js closest child is null javascript dom selector ancestor element javascript get nextparentelement select an elements ancestor in the dom closest() Method this how to find closest parent element in javascript jquery this closest el.closest js closest w3 javascript get closest web component javascript get closest attribute get event closest \targety closest element in css js close match javascript find parent element closest find specific parent element javascript jquery closet mdn closest() event.currenttarget.closest javascript querySelector closest closset js closets element previous javascript get an element near above javascript closest() in javascript js find closest element javascript closest examples get closest js ancestor javascript find parent element by tag name javascript select parent element by type css closest next closest parent jquery example js closest() closest injquery how to select closest before element with javascript how to select closest element with javascript javascript nearest element javascript closest sibling js selecte nearest element get closest class javascript dom closest DOM ancestor element closest mdn HTMLElement closest mdn closest closeet js closest queryselector javascript.closest vanilla javascript closest label closest mdn Javascript currenTarget closest js closest element compabilyty closets javscript javscript find closest parent of element javscript find closest parent closet js clostest js find parent js js parent selector javascript closest selector get nearest parent jquery get closer parent li javascript css select closer elements javascript find parent element by class js element.closest selectors js.closest javascript clossest js closest paarent div js first paarent div javascript find closest class closestRelative() javascript find perent element js event.target closest javascript tag nearest js closestrelative function javascript find closest parent button js event.target.closest dom element nearest vanilla js closest element with class javascript find the closest elemtn div.closest('body') js find closest parent element css select closest type of element element parent and grandparent dom javascript .closest() closest() javascript .closets jquery js parent until how to find the grandparent element dom javascript element ancestor get element by parent class javascript js get closest uncle branch js get closest realated branch js get closest related parent js element find parent js eclosest equivelant js .closest w3schools element.closest javascript target.closest get closest active element js get element preceding closest javascript Element.closest() javascriptElement.closest() javascript get closest element by id javascript this closest havascript closest element.closest() this.closest js this.closest .closest css this.closest() in vanilla js element get closest by selector js dom closest js vanilla dom .closest vanilar js find nearest find parents javascript get closest div id javascript closest element js javascript closest element with class javascript target closest javascript check if parents has class js this.nearest get closest element javascript closest parent with class javascript closest in js .closest vanilla js find closest in javascript how to target nearest siblin element js closest class javascript var parent = $(this).closest('.header__item'); get closes elements jaascript get closest element by class javascript find last parent javascript javascript has parent nearest tag css selector this.closest get node find the closest ancestor css jquery.closest closest javascript get parent node by tag name find the closest up the dom vanilla jquery event closest event target closest closest div jquery find parent javascript queryselector in closest div javascript select closest parent document.queryselector closest javascript target closest to find dom parents react closest element to div javascript closest parent with class html closest parent html closest element js find parent e.target.closest html nearest closest javascirpt two closest events or javascript js gets closest parent with class closest selector jquery closest parent javascript closest div of a div in html match vs closest js javascript get nearest element by tag js search parent how to target anscestor element by id &quot;javascript&quot; closest not working javascript find closest element with class javascript closest with variable event.target closest variable ID js table closest go to specific closest node js javascript get specific parent element .closet meaning in javascript find closest li jquery js nearest element js find nesresest element js get first parent target closest javascript closet in javascript get html near js find closest parent with class javascript .closest in javascript how to find closest element in javascript search within parent node javascript closeset element jquery js find closest parent event.target.closest and goto js .closest javascript closest parent of type venella javascript find closest vanilla javascript closest element venella javascript closest nearest jquery jquery closetst vanilla js closest js vanilla nearest html closest table ie js find nearest element closest parent js closest child element of a particular type in javascript js find parent element javascript select parent with class select closest tag in javascript how to select closest element javascript closest parent element matching given selector js next closest parent element javascript javascript parent element by class get closest parent element javascript event.target.closest means closests jquery closet javascript javascript find nearest element with class find closest parent node div .matches vs closest js how to get to closest th Event.target closest js js closest parent .closest vs .matches javascript closest vs matches javascript js find closest javascript find ancestor this.closest javascript get nearest div element javascript get nearest element javascript js event target closest javascript get closest element javascript go to closest element .closest js dom api closest jquery closest( event.target closest javascript closet javascript find the nearest a tag closest element javascript javascript find parent element jquery closest event ancestor javascript event.target.closest javascript closest in javascript if contains javascript html closest closest() javascript example js get closest element in node list javascript closest function this.closest jquery .closest javascript javascript .closest event.target.closest .closest jquery closest javascript jquery closest element closest jquery javascript closest find parent element javascript jquery closes find nearest element js js closest javascript find closest element javascript find nearest element
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source