比较变量点与 JavaScript 中数组的最高匹配截止/阈值
Comparing Variable Points with Highest Matching cutoff/threshold of Array in JavaScript
在我的JavaScript示例中运行了一个变量var runs = 14;
我还有一个包含[{"10":"lowestscore"},{"13":"lowscore"},{"20":"okscore"}]
的阈值数组。
我想通过最高匹配运行阈值与运行来打印与变量对应的消息。此处的示例输出应为低分,因为 14 匹配,13 作为最高阈值。
请提出一种有效的计算方法。
我正在尝试
$.each(runsThresholdArray, function (index, value) {
while (value.key < runs) {
// some logic
}
})
;
谢谢
这样的事情应该可以解决问题:
var tresholds = [{"10":"lowestscore"},{"13":"lowscore"},{"20":"okscore"}];
var runs = 14;
var result = { value: 0 }; // Result variable, the end result is stored in this.
for(var i = 0; i < tresholds.length; i++){ // Iterate over all tresholds.
var value = parseInt(Object.keys(tresholds[i])[0]); // Get the current treshold's value.
if(value > result.value && value <= runs){ // If the value is higher than any previous result, but not too high,
result.value = value; // Remember the current result
result.text = tresholds[i][value];
}
}
alert(JSON.stringify(result)) // `result.value` is the number, `result.text` is the text.
如果没有 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);
效率应该不是问题,除非您在这里有数千个具有数千个阈值的呼叫。
我认为你浪费了一个非常好的数据结构来threshold
:一个对象。比对象数组更容易管理:
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 runsThresholdArray = **{{**"10":"lowestscore"},{"13":"lowscore"},{"20":"okscore"**}}**;
$.each(runsThresholdArray, function **(key, value)** {
while (**key** < runs) {
// some logic
}
});
您的错误:不是索引,值//使用:键,值index 用于未配对/未关联的数组:从概念上讲,您的数组是一个对象。使用 {} 而不是 [](请参阅我的代码)。顺便说一下,您将有一种更简单的方法来解析它。
JQuery doc (http://api.jquery.com/jquery.each/) :
var obj = {
"flammable": "inflammable",
"duh": "no duh"
};
$.each( obj, function( **key, value** ) {
alert( key + ": " + value );
});
相关文章:
- Highchart多阈值颜色
- WebAudio API麦克风分贝阈值/选通(麦克风输入太敏感)
- Highcharts.js设置用于更改列颜色的阈值
- 更改默认阈值或使用锤子 2(使用 jQuery 插件)锁定轴
- querySelectorAll:查找子级,直到遇到某个阈值元素
- 范围中匹配日期值的自定义ng筛选器
- 链接D3.js力布局网络的阈值函数中的笔划宽度
- 聚合物动态创建的元素不工作(铁卷轴阈值)
- 用rxjs通过阈值来压平数值序列
- 如何更新具有匹配选项值文本的选择文本
- 使用 JavaScript 累积具有阈值的价格
- 高图表根据值更改条形颜色
- 从 JavaScript 中的 2 个数组中获取不匹配的值
- AngularJS:在$scope中搜索匹配的值
- 如何在滚动阈值量后循环 100% 高度的页面和 URL
- D3.js 折线和面积图 - 想要添加一条由两点定义并表示阈值/最小值的额外线(以便于查看)
- 我可以根据图表中的给定阈值设置不同的填充颜色吗.js
- 改进正则表达式以匹配有效值
- 高图对数X轴在没有零值或低于零或最小值的阈值的情况下失效
- 比较变量点与 JavaScript 中数组的最高匹配截止/阈值