存储大量数据的更好方法

时间:2013-02-27 21:43:32

标签: javascript arrays function select

我正在制作价目表,我有很多数据需要解析。所有数据都不同,没有公式。以下是我正在处理的内容片段。我想知道是否有更好的方法来存储和访问所有这些数据。

    function getPrice(number) {
    totalPrice = 0;
    for (var t = 2; t <= number; t++) {
        var ipp = 'cab' + (t - 1) + 'Size';
        var cabP = 'p' + (t - 1);
        var cabStyle = document.getElementById('cab' + (t - 1)).value;
        var cabs = document.getElementById(ipp);
        switch (cabStyle) {
            case 'b1':
                if (cabs.value === "") {
                    totalPrice = totalPrice + 0;
                    document.getElementById(ipp).style.border = "1px solid red";
                } else if (cabs.value <= 12) {
                    totalPrice = totalPrice + 111;
                    document.getElementById(cabP).innerHTML = " $111";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 12 && cabs.value <= 13.5) {
                    totalPrice = totalPrice + 113;
                    document.getElementById(cabP).innerHTML = " $113";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 13.5 && cabs.value <= 15) {
                    totalPrice = totalPrice + 116;
                    document.getElementById(cabP).innerHTML = " $116";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 15 && cabs.value <= 16.5) {
                    totalPrice = totalPrice + 118;
                    document.getElementById(cabP).innerHTML = " $118";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 16.5 && cabs.value <= 18) {
                    totalPrice = totalPrice + 120;
                    document.getElementById(cabP).innerHTML = " $120";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 18 && cabs.value <= 19.5) {
                    totalPrice = totalPrice + 122;
                    document.getElementById(cabP).innerHTML = " $122";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 19.5 && cabs.value <= 21) {
                    totalPrice = totalPrice + 124;
                    document.getElementById(cabP).innerHTML = " $124";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 21 && cabs.value <= 22.5) {
                    totalPrice = totalPrice + 126;
                    document.getElementById(cabP).innerHTML = " $126";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 22.5 && cabs.value <= 24) {
                    totalPrice = totalPrice + 141;
                    document.getElementById(cabP).innerHTML = " $141";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 24 && cabs.value <= 27) {
                    totalPrice = totalPrice + 146;
                    document.getElementById(cabP).innerHTML = " $146";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 27 && cabs.value <= 30) {
                    totalPrice = totalPrice + 152;
                    document.getElementById(cabP).innerHTML = " $152";
                    document.getElementById(ipp).style.border = "";
                }
                break;
            case 'b2':
                if (cabs.value === "") {
                    totalPrice = totalPrice + 0;
                    document.getElementById(ipp).style.border = "1px solid red";
                } else if (cabs.value <= 27) {
                    totalPrice = totalPrice + 167;
                    document.getElementById(cabP).innerHTML = " $167";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 27 && cabs.value <= 30) {
                    totalPrice = totalPrice + 173;
                    document.getElementById(cabP).innerHTML = " $173";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 30 && cabs.value <= 33) {
                    totalPrice = totalPrice + 178;
                    document.getElementById(cabP).innerHTML = " $178";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 33 && cabs.value <= 36) {
                    totalPrice = totalPrice + 183;
                    document.getElementById(cabP).innerHTML = " $183";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 36 && cabs.value <= 39) {
                    totalPrice = totalPrice + 194;
                    document.getElementById(cabP).innerHTML = " $194";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 39 && cabs.value <= 42) {
                    totalPrice = totalPrice + 199;
                    document.getElementById(cabP).innerHTML = " $199";
                    document.getElementById(ipp).style.border = "";
                } else if (cabs.value > 42 && cabs.value <= 45) {
                    totalPrice = totalPrice + 205;
                    document.getElementById(cabP).innerHTML = " $205";
                    document.getElementById(ipp).style.border = "";
                }
                break;

1 个答案:

答案 0 :(得分:1)

重新排列部分代码可能有所帮助。例如,document.getElementById(ipp).style.border行在所有块中都是相同的,除了第一行。所以你可以把它拿出来并单独阻止它:

document.getElementById(ipp).style.border = cabs.value ? "" : "1px solid red";

然后看起来您的b1出租车可以像这样排列:

cabdata = [111,113,116,118,120,122,124,126,141,146,146,152,152];
// note duplicate values at end due to bigger interval
cabindex = Math.max(0,Math.ceil((cabs.value-12)/1.5));
totalPrice += cabdata[cabindex];
document.getElementById(cabP).innerHTML = " $"+cabdata[cabindex];

同样适用于b2

cabdata = [167,173,178,183,194,199,205];
cabindex = Math.max(0,Math.ceil((cabs.value-27)/3));
totalPrice += cabdata[cabindex];
document.getElementById(cabP).innerHTML = " $"+cabdata[cabindex];

另请注意,上述两个块中的最后两行相同,因此可以将它们移动到switch之后。

总有一个公式,虽然它可能不是那么简单。示例:Batman formula