如何将输入值与对象文字中的引用值进行比较?
How to I compare an input value to reference values in an object literal?
我想将用户输入值与对象文字中排列的一组值进行比较。用户将输入两个值,一个参考变量 wk
和一个 number
。那些需要在对象文字中交叉引用,但我似乎无法弄清楚这样做的逻辑?
对象字面量构造如下:
var reference = {
wk31: {
_90th: '758',
_75th: '683',
_50th: '600',
_25th: '516',
_10th: '441'
},
wk32: {
_90th: '815',
_75th: '734',
_50th: '644',
_25th: '554',
_10th: '472'
},
wk33: {
_90th: '870',
_75th: '783',
_50th: '687',
_25th: '590',
_10th: '503'
}
}
例如,如果
wk = 32
& number = 755
,那么我想让用户知道他们的 number is between the 75th and 90th percentile for that wk value
.
感谢任何帮助。谢谢。
var reference = {
wk31: {
_90th: '758',
_75th: '683',
_50th: '600',
_25th: '516',
_10th: '441'
},
wk32: {
_90th: '815',
_75th: '734',
_50th: '644',
_25th: '554',
_10th: '472'
},
wk33: {
_90th: '870',
_75th: '783',
_50th: '687',
_25th: '590',
_10th: '503'
}
}
function checkReference()
{
var input = "wk" + document.querySelector("#week_input").value;
var perc = document.querySelector("#numberInput").value;
var obj = reference[input];
var keys = Object.keys(obj);
var string = "";
for (var i = 0; i < keys.length; i++)
{
if (perc > obj[keys[i]])
{
if (i == 0)
{
string = "number higher than 90th percentile for that wk value.";
break;
}
else
{
string = "number is between "+keys[i].substr(1)+" and "+ keys[i-1].substr(1) +" percentile for that wk value.";
break;
}
}
else if (perc < obj[keys[i]] && i == keys.length-1)
{
string = "number lower than 10th percentile for that wk value.";
}
}
alert(string);
}
document.querySelector("#doCheck").addEventListener("click", checkReference, false);
<input id="week_input" type="range" min="31" max="33">
<input id="numberInput" type="range" min="0" max="1000"><br />
<button id="doCheck">Check</button>
像这样:
用户将值滑动到位。代码检查对象和 returns 值。滑块用于大大减少用户输入错误。
HTML
<div id="result"></div>
JS
var reference = {
wk31: {
_90th: '758',
_75th: '683',
_50th: '600',
_25th: '516',
_10th: '441'
},
wk32: {
_90th: '815',
_75th: '734',
_50th: '644',
_25th: '554',
_10th: '472'
},
wk33: {
_90th: '870',
_75th: '783',
_50th: '687',
_25th: '590',
_10th: '503'
}
}
var result = $("#result");
//wk value
var bracket = 32;
//value to factor into percentile
var value = 740;
var percentileResult = "";
for (var i in reference) {
if (reference.hasOwnProperty(i) && i.indexOf("wk" + bracket) !== -1) {
var ref = reference[i];
for (var j in ref) {
if (ref.hasOwnProperty(j)) {
var percentileValue = ref[j];
if (percentileValue < value) {
percentileResult = j;
break;
}
}
}
}
};
result.html(value + " is in the " + percentileResult.replace("_", "") + " percentile of wk" + bracket);
Fiddle:
http://jsfiddle.net/7avjhvsy/
如果您可能有 wk 值,例如 wk31 和 wk311,请考虑分别在循环之前和第一个循环的 if 语句中添加以下代码片段。在这种情况下,第二个条件对两者的计算结果都为真并选择一个不正确的值:
regex = new RegExp("wk(/\d{" + bracket.toString().length + "})", "g");
if (reference.hasOwnProperty(i) && i.indexOf("wk" + bracket) !== -1 && regex.test(i)) {
Javascript
function findUpperPercentile(wk, number, reference){
var upperPercentile = "_100th";
var wkArray = reference['wk'+wk];
for (var i in wkArray ){
if (wkArray[i]>number){
upperPercentile=i
}
}
return upperPercentile;
}
根据 here and here 所写的内容,您似乎不能依赖 Object.keys()
返回的数组顺序和 for ... in
循环枚举对象的顺序属性。
相反,将数据重新排列成更有用的东西。
首先,使用以下方法获取 wk
值的正确对象:
var wkObj = reference['wk' + wk];
然后使用以下方法重新排列这些值:
var percentiles = $.map(wkObj, function(value, key) {
return { number: Number(value), percentile: key.substr(1) };
}).sort(function(a, b) { return a.number - b.number; });
这将创建一个对象数组,其中每个对象都有两个属性:(1) number
,它是一个数字,和 (2) percentile
,它是一个字符串。该数组按 number
值升序排序。
例如,percentiles
数组对于 wk = 32
如下所示:
[
{ number: 472, percentile: '10th' },
{ number: 554, percentile: '25th' },
{ number: 644, percentile: '50th' },
{ number: 734, percentile: '75th' },
{ number: 815, percentile: '90th' }
]
现在 number
值是数字,而不是字符串,您可以安全地按所需顺序遍历数组。
var upperIndex = -1;
$.each(percentiles, function(i, obj) {
if (obj.number > number) {
upperIndex = i;
return false;
}
});
这里有一个 jsfiddle 显示上面代码的使用。
我想将用户输入值与对象文字中排列的一组值进行比较。用户将输入两个值,一个参考变量 wk
和一个 number
。那些需要在对象文字中交叉引用,但我似乎无法弄清楚这样做的逻辑?
对象字面量构造如下:
var reference = {
wk31: {
_90th: '758',
_75th: '683',
_50th: '600',
_25th: '516',
_10th: '441'
},
wk32: {
_90th: '815',
_75th: '734',
_50th: '644',
_25th: '554',
_10th: '472'
},
wk33: {
_90th: '870',
_75th: '783',
_50th: '687',
_25th: '590',
_10th: '503'
}
}
例如,如果
wk = 32
& number = 755
,那么我想让用户知道他们的 number is between the 75th and 90th percentile for that wk value
.
感谢任何帮助。谢谢。
var reference = {
wk31: {
_90th: '758',
_75th: '683',
_50th: '600',
_25th: '516',
_10th: '441'
},
wk32: {
_90th: '815',
_75th: '734',
_50th: '644',
_25th: '554',
_10th: '472'
},
wk33: {
_90th: '870',
_75th: '783',
_50th: '687',
_25th: '590',
_10th: '503'
}
}
function checkReference()
{
var input = "wk" + document.querySelector("#week_input").value;
var perc = document.querySelector("#numberInput").value;
var obj = reference[input];
var keys = Object.keys(obj);
var string = "";
for (var i = 0; i < keys.length; i++)
{
if (perc > obj[keys[i]])
{
if (i == 0)
{
string = "number higher than 90th percentile for that wk value.";
break;
}
else
{
string = "number is between "+keys[i].substr(1)+" and "+ keys[i-1].substr(1) +" percentile for that wk value.";
break;
}
}
else if (perc < obj[keys[i]] && i == keys.length-1)
{
string = "number lower than 10th percentile for that wk value.";
}
}
alert(string);
}
document.querySelector("#doCheck").addEventListener("click", checkReference, false);
<input id="week_input" type="range" min="31" max="33">
<input id="numberInput" type="range" min="0" max="1000"><br />
<button id="doCheck">Check</button>
像这样:
用户将值滑动到位。代码检查对象和 returns 值。滑块用于大大减少用户输入错误。
HTML
<div id="result"></div>
JS
var reference = {
wk31: {
_90th: '758',
_75th: '683',
_50th: '600',
_25th: '516',
_10th: '441'
},
wk32: {
_90th: '815',
_75th: '734',
_50th: '644',
_25th: '554',
_10th: '472'
},
wk33: {
_90th: '870',
_75th: '783',
_50th: '687',
_25th: '590',
_10th: '503'
}
}
var result = $("#result");
//wk value
var bracket = 32;
//value to factor into percentile
var value = 740;
var percentileResult = "";
for (var i in reference) {
if (reference.hasOwnProperty(i) && i.indexOf("wk" + bracket) !== -1) {
var ref = reference[i];
for (var j in ref) {
if (ref.hasOwnProperty(j)) {
var percentileValue = ref[j];
if (percentileValue < value) {
percentileResult = j;
break;
}
}
}
}
};
result.html(value + " is in the " + percentileResult.replace("_", "") + " percentile of wk" + bracket);
Fiddle: http://jsfiddle.net/7avjhvsy/
如果您可能有 wk 值,例如 wk31 和 wk311,请考虑分别在循环之前和第一个循环的 if 语句中添加以下代码片段。在这种情况下,第二个条件对两者的计算结果都为真并选择一个不正确的值:
regex = new RegExp("wk(/\d{" + bracket.toString().length + "})", "g");
if (reference.hasOwnProperty(i) && i.indexOf("wk" + bracket) !== -1 && regex.test(i)) {
Javascript
function findUpperPercentile(wk, number, reference){
var upperPercentile = "_100th";
var wkArray = reference['wk'+wk];
for (var i in wkArray ){
if (wkArray[i]>number){
upperPercentile=i
}
}
return upperPercentile;
}
根据 here and here 所写的内容,您似乎不能依赖 Object.keys()
返回的数组顺序和 for ... in
循环枚举对象的顺序属性。
相反,将数据重新排列成更有用的东西。
首先,使用以下方法获取 wk
值的正确对象:
var wkObj = reference['wk' + wk];
然后使用以下方法重新排列这些值:
var percentiles = $.map(wkObj, function(value, key) {
return { number: Number(value), percentile: key.substr(1) };
}).sort(function(a, b) { return a.number - b.number; });
这将创建一个对象数组,其中每个对象都有两个属性:(1) number
,它是一个数字,和 (2) percentile
,它是一个字符串。该数组按 number
值升序排序。
例如,percentiles
数组对于 wk = 32
如下所示:
[
{ number: 472, percentile: '10th' },
{ number: 554, percentile: '25th' },
{ number: 644, percentile: '50th' },
{ number: 734, percentile: '75th' },
{ number: 815, percentile: '90th' }
]
现在 number
值是数字,而不是字符串,您可以安全地按所需顺序遍历数组。
var upperIndex = -1;
$.each(percentiles, function(i, obj) {
if (obj.number > number) {
upperIndex = i;
return false;
}
});
这里有一个 jsfiddle 显示上面代码的使用。