比较JavaScript中数组的最高匹配截止值/阈值的变量点

比较JavaScript中数组的最高匹配截止值/阈值的变量点,javascript,json,Javascript,Json,在我的JavaScript示例中有一个变量运行var runs=14还有一个阈值数组,它包含[{“10”:“lowstcore”}、{“13”:“lowscore”}、{“20”:“okscore”}] 我想打印与变量相对应的消息,该消息与运行的最高匹配运行阈值一致。这里的示例输出应该是低分,14匹配,13作为最高阈值 请建议一种有效的计算方法 我在试着用 $.each(runsThresholdArray, function (index, value) { while (value.key

在我的JavaScript示例中有一个变量运行
var runs=14还有一个阈值数组,它包含
[{“10”:“lowstcore”}、{“13”:“lowscore”}、{“20”:“okscore”}]

我想打印与变量相对应的消息,该消息与运行的最高匹配运行阈值一致。这里的示例输出应该是低分,14匹配,13作为最高阈值

请建议一种有效的计算方法

我在试着用

$.each(runsThresholdArray, function (index, value) {
 while (value.key <  runs) {
             // some logic
              }
             })

;
$。每个(运行ThresholdArray,函数(索引,值){
while(value.key

感谢没有jQuery,假设数组没有排序,但内容格式良好,只包含此处显示的单个属性

var threshold =  [{"10":"lowestscore"},{"13":"lowscore"},{"20":"okscore"}],
    runs = 14;

var getMessage = function (threshold, runs) {
    var onlyPropertyToInt = function (obj) {
        return parseInt(Object.getOwnPropertyNames(obj));
    };

    return threshold.sort(function(a,b){
            return onlyPropertyToInt(a)-onlyPropertyToInt(b)
        }).reduce(function (memo, item){
            var num = onlyPropertyToInt(item);
            if (num < runs) { memo = item[''+num] }
            return memo;
        }, null);
}

getMessage(threshold, runs);
var阈值=[{“10”:“最低分数”}、{“13”:“最低分数”}、{“20”:“okscore”}],
运行次数=14次;
var getMessage=函数(阈值,运行){
var onlyPropertyToInt=函数(obj){
返回parseInt(Object.getOwnPropertyNames(obj));
};
返回阈值。排序(函数(a,b){
仅返回属性点(a)-仅返回属性点(b)
}).减少(功能(备忘录,项目){
var num=仅属性点(项目);
如果(num

效率不应该是一个问题,除非您在这里有数千个具有数千个阈值的呼叫。

类似这样的方法应该可以解决这个问题:

var tresholds=[{“10”:“最低分数”}、{“13”:“最低分数”}、{“20”:“okscore”}];
var=14;
var result={value:0};//结果变量,最终结果存储在该变量中。
对于(var i=0;i如果(value>result.value&&value我认为你在浪费一个完美的数据结构来满足你的
阈值:一个对象。比一个对象数组更容易管理:

var threshold = {
    45: "hooray" ,
    13: "lowscore" ,
    20: "okscore" ,
    100: "god like" ,
    10: "lowestscore",
    25: "notbad",
    2: "uh oh"
}

var runs = 100;

function getThreshold(runs, threshold) {

    // get a list of your object keys, convert them to an integer
    // and then sort them
    var keys = Object.keys(threshold)
                     .map(Number)
                     .sort(function (a, b) { return a - b; });

    // loop over the keys
    for (var i = 0, l = keys.length; i < l; i++) {

        // get the difference between `runs` and the current
        // element, and `runs` and the next element
        var diff = Math.abs(runs - keys[i]);
        var diffNext = Math.abs(runs - keys[i + 1]);

        // store the resulting notification from the
        // threshold object
        var tmp = threshold[keys[i]];

        // if the current difference is greater than the
        // difference of the next element, continue to the
        // next element, otherwise break out of the loop
        // and return the notification
        if (diff <= diffNext) { break; }
    }
    return tmp;
}

getThreshold(100, threshold); // god like
getThreshold(14, threshold); // lowscore
getThreshold(2, threshold); // uh oh
getThreshold(24, threshold); // notbad
var阈值={
45:“万岁”,
13:“低分”,
20:“okscore”,
100:“上帝般”,
10:“最低分数”,
25:“不错”,
2:“嗯哦”
}
var=100;
函数getThreshold(运行,阈值){
//获取对象键的列表,将其转换为整数
//然后把它们分类
var keys=Object.keys(阈值)
.地图(编号)
.sort(函数(a,b){返回a-b;});
//在钥匙上打圈
对于(变量i=0,l=keys.length;iif(diff
if(diff)循环中似乎还有剩余的
out=null
,在那里。
var threshold =  [{"10":"lowestscore"},{"13":"lowscore"},{"20":"okscore"}],
    runs = 14;

var getMessage = function (threshold, runs) {
    var onlyPropertyToInt = function (obj) {
        return parseInt(Object.getOwnPropertyNames(obj));
    };

    return threshold.sort(function(a,b){
            return onlyPropertyToInt(a)-onlyPropertyToInt(b)
        }).reduce(function (memo, item){
            var num = onlyPropertyToInt(item);
            if (num < runs) { memo = item[''+num] }
            return memo;
        }, null);
}

getMessage(threshold, runs);
var threshold = {
    45: "hooray" ,
    13: "lowscore" ,
    20: "okscore" ,
    100: "god like" ,
    10: "lowestscore",
    25: "notbad",
    2: "uh oh"
}

var runs = 100;

function getThreshold(runs, threshold) {

    // get a list of your object keys, convert them to an integer
    // and then sort them
    var keys = Object.keys(threshold)
                     .map(Number)
                     .sort(function (a, b) { return a - b; });

    // loop over the keys
    for (var i = 0, l = keys.length; i < l; i++) {

        // get the difference between `runs` and the current
        // element, and `runs` and the next element
        var diff = Math.abs(runs - keys[i]);
        var diffNext = Math.abs(runs - keys[i + 1]);

        // store the resulting notification from the
        // threshold object
        var tmp = threshold[keys[i]];

        // if the current difference is greater than the
        // difference of the next element, continue to the
        // next element, otherwise break out of the loop
        // and return the notification
        if (diff <= diffNext) { break; }
    }
    return tmp;
}

getThreshold(100, threshold); // god like
getThreshold(14, threshold); // lowscore
getThreshold(2, threshold); // uh oh
getThreshold(24, threshold); // notbad