JavaScript - 关于数据结构的问题
我正在尝试根据积分系统计算某人的肠道健康状况。我可以以任何方式编辑数据结构或逻辑。我只是想编写一个函数和数据结构来处理这种能力。
伪计算器函数:
// Bowel health calculator
var points = 0;
If age is from 30 and 34:
points += 1
If age is from 35 and 40:
points += 2
If daily BMs is from 1 and 3:
points -= 1
If daily BMs is from 4 and 6:
points -= 2
return points;
伪数据结构:
var points_map = {
age:
{
'35-40': 1,
'40-45': 2,
'45-50': 6,
'50-55': 2,
'55-60': 1,
'60-65': 4,
'65-70': 3,
'70-75': 1,
'75-150': 2
},
dbm:
{
'1-3': -1,
'4-6': -2,
'7-9': -3,
'10-150': 5
}
// This plus about 10 more metrics like them (all with the same "Map a plus or minus value to each range" logic)
};
我有一个像这样的完整数据电子表格,我正在尝试编写代码的DRY版本和该数据的DRY版本(即,可能不是“30-34”的字符串)等)以便在没有大量 switch
语句的情况下处理此类事情。
I'm trying to calculate somebody's bowel health based on a points system. I can edit the data structure, or logic in any way. I'm just trying to write a function and data structure to handle this ability.
Pseudo calculator function:
// Bowel health calculator
var points = 0;
If age is from 30 and 34:
points += 1
If age is from 35 and 40:
points += 2
If daily BMs is from 1 and 3:
points -= 1
If daily BMs is from 4 and 6:
points -= 2
return points;
Pseudo data structure:
var points_map = {
age:
{
'35-40': 1,
'40-45': 2,
'45-50': 6,
'50-55': 2,
'55-60': 1,
'60-65': 4,
'65-70': 3,
'70-75': 1,
'75-150': 2
},
dbm:
{
'1-3': -1,
'4-6': -2,
'7-9': -3,
'10-150': 5
}
// This plus about 10 more metrics like them (all with the same "Map a plus or minus value to each range" logic)
};
I have a full spreadsheet of data like this, and I am trying to write a DRY version of code and a DRY version of this data (ie, probably not a string for the '30-34', etc) in order to handle this sort of thing without a humongous number of switch
statements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为更好的结构可能有这样的东西:
然后在上面做一个循环:
如果需要的话,这可以变得更有效,但你明白了。
I'd think a better structure might have something like:
then do a loop on it:
This could be made a little more efficient if needed but you get the idea.