我想创建一个函数,该函数将从以下列表中检索结果
var Fedsy = [
{min : 0, max : 2300 , must:0, percent:0 , excess:0},
{min : 2300, max : 11525 , must:0, percent:10 , excess:2300},
{min : 11525, max : 39750 , must:922.50, percent:15 , excess:11525},
{min : 39750, max : 93050 , must:5156.25, percent:25 , excess:39750},
{min : 93050, max : 191600 , must:18481.25, percent:28 , excess:93050},
{min : 191600, max : 413800 , must:46075.25, percent:33 , excess:191600},
{min : 413800, max : 415500 , must:119401.25, percent:35 , excess:413800},
{min : 415500, max : 1000000000 , must:119996.25, percent:39.6 , excess:415500}
];
现在,如果“ inputNum”介于“ min”和“ max”之间,我想从列表中拉出该行
var result= function(inputNum){
///..???
}
例如:如果“ inputNum” = 40000,则该函数应返回
{min : 39750, max : 93050 , must:5156.25, percent:25 , excess:39750}
既然您已标记underscore.js
,我知道您想使用该库解决问题。
这样,您可以使用该函数_.find
查找与条件匹配的第一个值。可以将其视为一个过滤器,但只需查看第一个过滤器即可:
function findRange(inputNum) {
return _.find(Fedsy, function(f) {
return f.min <= inputNum && f.max >= inputNum;
});
}
Fedsy(f
)中要true
在迭代器函数中返回的第一项将是下划线返回的值。
您可以在underscore.js网站上找到它的详细信息和更多参考功能:http : //underscorejs.org/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句