javascript Javascript在数组中找到最接近的数字而不去
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25061543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript find closest number in array without going under
提问by Amo
I have an array of numbers, for example [300, 500, 700, 1000, 2000, 3000]
and I want to find the closest number, without going under the number given.
例如[300, 500, 700, 1000, 2000, 3000]
,我有一个数字数组,我想找到最接近的数字,而不是在给定的数字之下。
For instance, searching for 2200 would return 3000 (NOT 2000).
例如,搜索 2200 将返回 3000(不是 2000)。
However, if I search for 3200 as there is nothing higher in the array, it should return 3000 as there are no other choices.
但是,如果我搜索 3200,因为数组中没有更高的值,它应该返回 3000,因为没有其他选择。
I can get the closest number that is under the value using:
我可以使用以下方法获得最接近该值的数字:
if (sizeToUse == null || Math.abs(this - monitorWidth) < Math.abs(sizeToUse - monitorWidth)) {
sizeToUse = this;
}
However, I can't get the whole thing to work. My full code is:
但是,我无法让整个事情发挥作用。我的完整代码是:
$(function() {
var monitorWidth = window.screen.availWidth,
sizeToUse = null,
upscaleImages = false;
$('.responsive-img').each(function(){
var sizeData = $(this).attr('data-available-sizes');
sizeData = sizeData.replace(' ', '');
var sizesAvailable = sizeData.split(',');
sizesAvailable.sort(function(a, b){return b-a});
$.each(sizesAvailable, function(){
if(upscaleImages){
if (sizeToUse == null || Math.abs(this - monitorWidth) < Math.abs(sizeToUse - monitorWidth)) {
sizeToUse = this;
}
}
else{
//We don't want to upscale images so we need to find the next highest image available
}
});
console.log('Size to use ' + sizeToUse + ' monitor width ' + monitorWidth);
});
});
采纳答案by Karl-André Gagnon
You can use this code :
您可以使用此代码:
function closest(arr, closestTo){
var closest = Math.max.apply(null, arr); //Get the highest number in arr in case it match nothing.
for(var i = 0; i < arr.length; i++){ //Loop the array
if(arr[i] >= closestTo && arr[i] < closest) closest = arr[i]; //Check if it's higher than your number, but lower than your closest value
}
return closest; // return the value
}
var x = closest(yourArr, 2200);
Fiddle : http://jsfiddle.net/ngZ32/
回答by Jeremy J Starcher
var list = [300, 500, 700, 1000, 2000, 3000];
function findBestMatch(toMatch) {
// Assumes the array is sorted.
var bestMatch = null;
var max = Number.MIN_VALUE;
var item;
for (var i = 0; i < list.length; i++) {
item = list[i];
if (item > toMatch) {
bestMatch = item;
break;
}
max = Math.max(max, item);
}
// Compare to null, just in case bestMatch is 0 itself.
if (bestMatch !== null) {
return bestMatch;
}
return max;
}
alert(findBestMatch(2200));
alert(findBestMatch(3200));
回答by James Curran
sizesAvailable.sort(function(a, b){return a-b}); // DESCENDING sort
if(upscaleImages) // do th eif once, not every time through the loop
{
$.each(sizesAvailable, function()
{
if (this > monitorWidth)
sizeToUse = this;
}
if (sizeToUse == null) sizeToUse = sizesAvailable[0];
}
else
{
$.each(sizesAvailable, function()
{
//We don't want to upscale images so....
}
}
});