admin管理员组文章数量:1291805
I have a javascript array like this
var array1= [10,20,30,40,50];
Is there any method using which i can get the closest array element to a given number ? Ex: if i pass 26, It should return 30 ( 26 is closest to 30). If i pass 42, It should return 40.
Any thoughts ? Should i iterate thru each elements ? Is there any methods available for this in jQuery ?
I have a javascript array like this
var array1= [10,20,30,40,50];
Is there any method using which i can get the closest array element to a given number ? Ex: if i pass 26, It should return 30 ( 26 is closest to 30). If i pass 42, It should return 40.
Any thoughts ? Should i iterate thru each elements ? Is there any methods available for this in jQuery ?
Share Improve this question asked Jul 1, 2011 at 18:13 ShyjuShyju 219k106 gold badges419 silver badges498 bronze badges 3- I don't believe there is anything in jquery which is going to help you with this. Are the elements guaranteed to be sorted? – James Montagne Commented Jul 1, 2011 at 18:15
- I don't think JQuery has any function that solves this problem. Furthermore, if array is unordered, you would need to iterate through all the array and find the answer. – user482594 Commented Jul 1, 2011 at 18:17
- Just sort it and loop through it.. really no built in function for jquery – shernshiou Commented Jul 1, 2011 at 18:27
5 Answers
Reset to default 6Simple with a for loop. No jQuery magic necessary:
function getClosestNum(num, ar)
{
var i = 0, closest, closestDiff, currentDiff;
if(ar.length)
{
closest = ar[0];
for(i;i<ar.length;i++)
{
closestDiff = Math.abs(num - closest);
currentDiff = Math.abs(num - ar[i]);
if(currentDiff < closestDiff)
{
closest = ar[i];
}
closestDiff = null;
currentDiff = null;
}
//returns first element that is closest to number
return closest;
}
//no length
return false;
}
If performance is a concern (very large array) and the array is ordered (as in the example), you may want to consider a Binary Search. You can probably find one pre-written for javascript but may need to modify slightly to handle your "closest" piece once the algorithm reaches the end.
Because you have ranges of 10 that are sequential, it can be as simple as this:
var array1= [10,20,30,40,50];
var n = 23;
var idx = Math.max( Math.round( n / 10 ) - 1, 0);
Examples:
var array1= [10,20,30,40,50];
var n = 23;
var idx = Math.max( Math.round( n / 10 ) - 1, 0);
alert( array1[ idx ] ); // 20
var n = 28;
var idx = Math.max( Math.round( n / 10 ) - 1, 0);
alert( array1[ idx ] ); // 30
var n = 1;
var idx = Math.max( Math.round( n / 10 ) - 1, 0);
alert( array1[ idx ] ); // 10
Examples: http://jsfiddle/GTrNt/
Should work the same with other ranges, like 25:
var array1= [25,50,75,100,125];
var n = 23;
var idx = Math.max( Math.round( n / 25 ) - 1, 0);
alert( array1[ idx ] ); // 25
var n = 48;
var idx = Math.max( Math.round( n / 25 ) - 1, 0);
alert( array1[ idx ] ); // 50
var n = 1;
var idx = Math.max( Math.round( n / 25 ) - 1, 0);
alert( array1[ idx ] ); // 25
Examples: http://jsfiddle/GTrNt/1
http://jsfiddle/nwUbV/1/
With binary search. If it's a really big array though, you'll need to implement a better sorting function such as Quicksort so that performance doesn't suffer.
I used jQuery to create the loop (just because I always have jQuery loaded) - it's not really necessary. And this may not be the most efficient method available. But here's a quick function that will do what you need:
var array1 = Array(20, 30, 40, 50);
var closestindex = findClosest(42, array1);
alert('Closest is '+array1[closestindex]);
function findClosest(needle, haystack) {
var offset = 10000;
var closest = 0;
$.each(haystack, function(i) {
if (haystack[i] <= needle) {
var localoffset = needle - haystack[i];
} else {
var localoffset = haystack[i] - needle;
}
if (localoffset <= offset) {
offset = localoffset;
closest = i;
}
});
return closest;
}
本文标签: How to get the closest element from a javascript array using jQueryStack Overflow
版权声明:本文标题:How to get the closest element from a javascript array using jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741540319a2384283.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论