找到最接近的较小值的数组

时间:2021-12-12 21:32:12

I think what I want is pretty simple but I can't really find the correct solution.

我想我想要的很简单,但我找不到正确的解决方案。

I have this kind of array in Javascript :

我在Javascript中有这种数组:

[0, 38, 136, 202, 261, 399]

And I get a generated value from 0 to 600 on a button click. What I need is to find the nearest lower value in this array.

按下按钮,我得到0到600的生成值。我需要的是找到这个数组中最接近的较低值。

For example, if the generated value is 198, I want to get 136 as the result. If the generated value is 300, I want 261... If it's 589, I want 399 etc etc.

例如,如果生成的值为198,我希望得到136作为结果。如果生成的值是300,我想要261 ...如果它是589,我想要399等等。

Until now, I have tried with this code :

到目前为止,我已尝试使用此代码:

var theArray = [ 1, 3, 8, 10, 13 ];
var goal = 7;
var closest = null;

$.each(theArray, function(){
    if (closest == null || Math.abs(this - goal) < Math.abs(closest - goal)) {
        closest = this;
    }
});

alert(closest);

But it only returns the closest value... Now I need the to get only the closest smaller value for the given number... How can I improve my algorithm to fit my needs?

但它只返回最接近的值...现在我需要得到给定数字的最接近的较小值...我如何改进我的算法以满足我的需要?

Thanks!

3 个解决方案

#1


3  

If you array is sorted, and small enough, a really simple mode to do what you want it's simplly iterate over the array until number > number-in-array then return the number on the previous position.

如果你对数组进行了排序,并且足够小,那么一个非常简单的模式可以做你想要的,它简单地迭代数组,直到number> number-in-array然后返回前一个位置的数字。

function getClosestValue(myArray, myValue){
    //optional
    var i = 0;

    while(myArray[++i] < myValue);

    return myArray[--i];
}

Regards.

#2


0  

Reverse the array and use find

反转数组并使用find

let arr = [0, 38, 136, 202, 261, 399];
let val = 300;
let number = arr.reverse().find(e => e <= val);
console.log(number);

#3


0  

You could use Array#some and exit if the item is greater or equal to the wanted value. Otherwise assign the actual value as return value.

您可以使用Array#some并在项目大于或等于所需值时退出。否则将实际值指定为返回值。

This proposal works for sorted arrays.

此提议适用于已排序的数组。

function getClosest(array, value) {
    var closest;
    array.some(function (a) {
        if (a >= value) {
            return true;
        }
        closest = a;
    });
    return closest;
}

var array = [0, 38, 136, 202, 261, 399];

console.log(getClosest(array, 100)); //  38
console.log(getClosest(array, 198)); // 136
console.log(getClosest(array, 300)); // 261
console.log(getClosest(array, 589)); // 399

#1


3  

If you array is sorted, and small enough, a really simple mode to do what you want it's simplly iterate over the array until number > number-in-array then return the number on the previous position.

如果你对数组进行了排序,并且足够小,那么一个非常简单的模式可以做你想要的,它简单地迭代数组,直到number> number-in-array然后返回前一个位置的数字。

function getClosestValue(myArray, myValue){
    //optional
    var i = 0;

    while(myArray[++i] < myValue);

    return myArray[--i];
}

Regards.

#2


0  

Reverse the array and use find

反转数组并使用find

let arr = [0, 38, 136, 202, 261, 399];
let val = 300;
let number = arr.reverse().find(e => e <= val);
console.log(number);

#3


0  

You could use Array#some and exit if the item is greater or equal to the wanted value. Otherwise assign the actual value as return value.

您可以使用Array#some并在项目大于或等于所需值时退出。否则将实际值指定为返回值。

This proposal works for sorted arrays.

此提议适用于已排序的数组。

function getClosest(array, value) {
    var closest;
    array.some(function (a) {
        if (a >= value) {
            return true;
        }
        closest = a;
    });
    return closest;
}

var array = [0, 38, 136, 202, 261, 399];

console.log(getClosest(array, 100)); //  38
console.log(getClosest(array, 198)); // 136
console.log(getClosest(array, 300)); // 261
console.log(getClosest(array, 589)); // 399