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.
For instance, searching for 2200 would return 3000 (NOT 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.
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);
});
});
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.
For instance, searching for 2200 would return 3000 (NOT 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.
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);
});
});
Share
Improve this question
edited Nov 24, 2015 at 17:15
Jonatas Walker
14.2k6 gold badges56 silver badges82 bronze badges
asked Jul 31, 2014 at 14:29
AmoAmo
2,9445 gold badges27 silver badges48 bronze badges
3
- Could you remove the 3/4th of the code that has nothing to do with the question? – James Curran Commented Jul 31, 2014 at 14:33
- I've sorted the array using: sizesAvailable.sort(function(a, b){return b-a}); – Amo Commented Jul 31, 2014 at 14:35
- Possible duplicate of get closest number out of array – Dan Mindru Commented Oct 4, 2016 at 12:28
4 Answers
Reset to default 6You 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/
Another approach is to find the first candidate greater than or equal to the one you want, and take the result, returning the last element if there are no matches:
function closestNumberOver(x, arr) {
return arr.find(d => d >= x) || arr[arr.length - 1]
}
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));
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....
}
}
});