为 JavaScript 中的大型数据集编写 if-then-else 的最佳方法是什么?
What is the best way to write if-then-else for a large data set in JavaScript?
我正在尝试为 JavaScript 中的大型数据集编写条件语句。目标是根据年龄和生活方式计算卡路里(结果)摄入量。
这是一个例子:
HTML
<label>Enter Age (2 to 100)</label>
<input id="getAge" type="text" placeholder="" />
<label>Enter 1 for Sedentary, 2 for Moderate, 3 for Active</label>
<input id="getLifeStyle" type="text" placeholder="" />
JavaScript:
let age, lifestyle, calorie;
age = document.getElementById("getAge").value
lifestyle = document.getElementById("getLifeStyle").value
// two
if (age==2) {
if (lifestyle==1) {
calorie=1000
}
if (lifestyle==2) {
calorie=2000
}
....
// three
else if (age==3) {
if (lifestyle==1) {
calorie=1000
}
if (lifestyle==2) {
calorie=2000
}....
// four...and so, until 100
// Show calorie
console.log("Calorie: "+calorie)
这是我想要实现的目标:
基本上重复以上几次。我不是 JavaScript 方面的专家。虽然上面的方法可以很好地获得结果,但我认为这可能不是编写 if-then-else 的最有效方法。 JavaScript 中是否还有其他 高效 选项可以做到这一点?
按年龄索引的嵌套对象,然后按生活方式索引就可以了。
const calorieData = {
2: {
1: 1000,
2: 2000,
},
3: {
1: 1000,
2: 2000,
}
};
// ...
const calories = calorieData[age][lifestyle];
DRY-er 版本将使用数组代替,但由于基于 0 的索引,它可能需要稍微更混乱的代码。
const calorieData = [
// omit index 0
,
// omit index 1
,
[1000, 2000],
[1000, 2000],
// ...
];
// ...
// because lifestyle is 1-indexed
const calories = calorieData[age][lifestyle - 1];
最好和最短的替代方法是使用三元运算符
// two
{age == 2 && lifestyle == 1 && calorie = 1000}
{age == 2 && lifestyle == 2 && calorie = 2000}
//three
{age == 3 && lifestyle == 1 && calorie = 1000}
{age == 3 && lifestyle == 2 && calorie = 2000}
// until 100
const LIFE_STYLE = {
SEDENTARY: '1',
MODERATE: '2',
ACTIVE: '3'
}
const data = [
{ age: 2, lifeStyle: LIFE_STYLE.SEDENTARY, calories: 1000 },
{ age: 3, lifeStyle: LIFE_STYLE.SEDENTARY, calories: 1000 }
]
const getCaloryData = (age, lifestyle) => {
const entry = data.find(entry => entry.age === age && entry.lifeStyle === lifestyle)
return entry ? entry.calories : undefined;
}
复杂度更高但可读性更好
我正在尝试为 JavaScript 中的大型数据集编写条件语句。目标是根据年龄和生活方式计算卡路里(结果)摄入量。
这是一个例子:
HTML
<label>Enter Age (2 to 100)</label>
<input id="getAge" type="text" placeholder="" />
<label>Enter 1 for Sedentary, 2 for Moderate, 3 for Active</label>
<input id="getLifeStyle" type="text" placeholder="" />
JavaScript:
let age, lifestyle, calorie;
age = document.getElementById("getAge").value
lifestyle = document.getElementById("getLifeStyle").value
// two
if (age==2) {
if (lifestyle==1) {
calorie=1000
}
if (lifestyle==2) {
calorie=2000
}
....
// three
else if (age==3) {
if (lifestyle==1) {
calorie=1000
}
if (lifestyle==2) {
calorie=2000
}....
// four...and so, until 100
// Show calorie
console.log("Calorie: "+calorie)
这是我想要实现的目标:
基本上重复以上几次。我不是 JavaScript 方面的专家。虽然上面的方法可以很好地获得结果,但我认为这可能不是编写 if-then-else 的最有效方法。 JavaScript 中是否还有其他 高效 选项可以做到这一点?
按年龄索引的嵌套对象,然后按生活方式索引就可以了。
const calorieData = {
2: {
1: 1000,
2: 2000,
},
3: {
1: 1000,
2: 2000,
}
};
// ...
const calories = calorieData[age][lifestyle];
DRY-er 版本将使用数组代替,但由于基于 0 的索引,它可能需要稍微更混乱的代码。
const calorieData = [
// omit index 0
,
// omit index 1
,
[1000, 2000],
[1000, 2000],
// ...
];
// ...
// because lifestyle is 1-indexed
const calories = calorieData[age][lifestyle - 1];
最好和最短的替代方法是使用三元运算符
// two
{age == 2 && lifestyle == 1 && calorie = 1000}
{age == 2 && lifestyle == 2 && calorie = 2000}
//three
{age == 3 && lifestyle == 1 && calorie = 1000}
{age == 3 && lifestyle == 2 && calorie = 2000}
// until 100
const LIFE_STYLE = {
SEDENTARY: '1',
MODERATE: '2',
ACTIVE: '3'
}
const data = [
{ age: 2, lifeStyle: LIFE_STYLE.SEDENTARY, calories: 1000 },
{ age: 3, lifeStyle: LIFE_STYLE.SEDENTARY, calories: 1000 }
]
const getCaloryData = (age, lifestyle) => {
const entry = data.find(entry => entry.age === age && entry.lifeStyle === lifestyle)
return entry ? entry.calories : undefined;
}
复杂度更高但可读性更好