次のような 2 つのスライダーを使用する電卓を作成しています。
次のようなオブジェクトに格納されている一連の CPU および RAM データがあります。
var CloudPlans = {
small: {
id: 'small',
from: {
cpu: 1,
ram: 1
},
to: {
cpu: 2,
ram: 2
},
price: {
linux: 3490,
windows: 4190
}
},
medium: {
id: 'medium',
from: {
cpu: 2,
ram: 2
},
to: {
cpu: 4,
ram: 4
},
price: {
linux: 5600,
windows: 6300
}
},
large: {
id: 'large',
from: {
cpu: 4,
ram: 4
},
to: {
cpu: 6,
ram: 8
},
price: {
linux: 9500,
windows: 10200
}
},
[...more configs here]
}
スライダーの位置と値に基づいて、ユーザーが選択したプランを確認し、コンポーネントの価格を計算する必要があります。価格帯をチェックする関数は次のとおりです。
checkPlaninRange: function(cpuVal, ramVal) {
if(cpuVal >= CloudPlan.small.from.cpu && cpuVal <= CloudPlan.small.to.cpu ) {
return "small";
} else if (ramVal >= CloudPlan.small.from.cpu && ramVal <= CloudPlan.small.to.cpu) {
return "small";
}
}
ご覧のとおり、選択したプランを返す条件のほぼ無限のリストを処理します。条件またはケース ステートメント以外のコードに基づいて、これらのプラン構成の保存または選択を簡素化する方法はありますか?