Javascript find closest number in array without going under

前端 未结 3 1244
花落未央
花落未央 2021-01-12 02:59

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.

F

3条回答
  •  爱一瞬间的悲伤
    2021-01-12 03:24

     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....
        }
     }
    });
    

提交回复
热议问题