Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript在数组中查找最接近的数字,而不必_Javascript - Fatal编程技术网

Javascript在数组中查找最接近的数字,而不必

Javascript在数组中查找最接近的数字,而不必,javascript,Javascript,我有一个数字数组,例如[300500700100020003000],我想找到最接近的数字,而不必在给定的数字下面 例如,搜索2200将返回3000(不是2000) 但是,如果我搜索3200,因为数组中没有更高的值,它应该返回3000,因为没有其他选择 我可以使用以下方法获得该值下最接近的数字: if (sizeToUse == null || Math.abs(this - monitorWidth) < Math.abs(sizeToUse - monitorWidth)) {

我有一个数字数组,例如
[300500700100020003000]
,我想找到最接近的数字,而不必在给定的数字下面

例如,搜索2200将返回3000(不是2000)

但是,如果我搜索3200,因为数组中没有更高的值,它应该返回3000,因为没有其他选择

我可以使用以下方法获得该值下最接近的数字:

if (sizeToUse == null || Math.abs(this - monitorWidth) < Math.abs(sizeToUse - monitorWidth)) {
                sizeToUse = this;
            }
if(sizeToUse==null | | Math.abs(this-monitorWidth)
然而,我不能让整个事情都顺利进行。我的全部代码是:

$(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);

});


});
$(函数(){
var monitorWidth=window.screen.availWidth,
sizeToUse=null,
向上缩放图像=错误;
$('.responsive img')。每个(函数(){
var sizeData=$(this.attr('data-available-size');
sizeData=sizeData.替换(“”,”);
变量SizeAvailable=sizeData.split(',');
sort(函数(a,b){return b-a});
$.each(大小可用,函数(){
如果(放大图像){
if(sizeToUse==null | | Math.abs(this-monitorWidth)
您可以使用以下代码:

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);
功能最近(arr,closestTo){
var resest=Math.max.apply(null,arr);//如果不匹配,则获取arr中的最大值。
对于(var i=0;i=closestTo&&arr[i]
小提琴:

var列表=[300500700100020003000];
函数findBestMatch(toMatch){
//假设数组已排序。
var-bestMatch=null;
var max=Number.MIN_值;
var项目;
对于(变量i=0;i匹配){
最佳匹配=项目;
打破
}
最大值=数学最大值(最大值,项目);
}
//与null比较,以防bestMatch本身为0。
如果(最佳匹配!==null){
返回最佳匹配;
}
返回最大值;
}
警报(findBestMatch(2200));
警报(findBestMatch(3200));

另一种方法是找到第一个大于或等于所需的候选元素,并获取结果,如果不存在匹配项,则返回最后一个元素:

  function closestNumberOver(x, arr) {
    return arr.find(d => d >= x) || arr[arr.length - 1]
  }

可以删除与问题无关的代码的3/4吗?我已经使用sizesAvailable.sort(函数(a,b){return b-a})对数组进行了排序;它的可能副本似乎可以用最少的代码完成我所需要的工作。回答得很好。谢谢
 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....
    }
 }
});
  function closestNumberOver(x, arr) {
    return arr.find(d => d >= x) || arr[arr.length - 1]
  }