Answer the question
In order to leave comments, you need to log in
How to find the closest element of an array in Javascript?
Hello, tell me how you can find the array element closest to the variable (you need to get the value and index of the array element), example:
arr = [0, 2, 5, 10]; //array
x = 7; //variable
Accordingly, the closest element to the specified variable will be the element with value 5 and index 2.
Answer the question
In order to leave comments, you need to log in
In short, we write:
git remote rm origin
git remote add origin [email protected]:username/myapp.git
and then our path
var val=7;
[0, 2, 5, 10].reduce(function(a,c) {
return Math.abs(a-val) < Math.abs(c-val) ? a: c;
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question