Javascript价格矩阵数组

标签 javascript multidimensional-array

我需要根据价格矩阵的宽度和高度来计算窗帘价格。如何从数组而不是 if 语句进行计算?

function checkPrice() {

        var price = calculatePrice();       

        function calculatePrice(price) {

    var width = parseInt(document.getElementById('customid0').value);
    var height = parseInt(document.getElementById('customid1').value);

            // width up to 100
            if (width <= 100){

            if (height <=50){                           
             price = 67.22;                         
            }

            if (height >50 && height <=100){                            
             price = 103.34;                            
            }

            if (height >100 && height <=130){                           
             price = 133.11;                            
            }
            }

            // width between 101 and 125
            if (width > 100 && width <= 125){

            if (height <=50){                           
             price = 76.69;                         
            }

            if (height >50 && height <=100){                            
             price = 113.01;                            
            }

            if (height >100 && height <=130){                           
             price = 146.05;                    
            }
            }

            // width between 126 and 150
            if (width > 125 && width <= 150){

            if (height <=50){                           
             price = 83.69;                         
            }

            if (height >50 && height <=100){                            
             price = 124.74;                            
            }

            if (height >100 && height <=130){                           
             price = 161.28;                    
            }
            }

                return price;
            }

document.getElementById('product_addtocart_form').action = '<?php echo $this->getAddToCartUrl($_product) ?>&price='+price;
     optionsPrice.changePrice('options', price);
     optionsPrice.reload();  
     Price.changePrice(price);  
}

最佳答案

var width  = parseInt( document.getElementById('customid0').value, 10);
var height = parseInt( document.getElementById('customid1').value, 10);

var conditions = [
        [0, 100, [        // width = 0..100
            [0, 50, [     //     height = 0..50
                67.22     //         price = 67.02
            ]],
            [50, 100, [   //     height = 50..100
                103.34    //         price = 103.34
            ]],
            [100, 130, [  //     height = 100..130
                133.11    //         price = 133.11
            ]]
        ]],
        [101, 125, [      // width = 101..125
            [0, 50, [     //     height = 0..50
                76.69     //         ...
            ]],
            [51, 100, [
                113.01
            ]],
            [101, 130, [
                146.05
            ]]
        ]],
        [126, 150, [
            [0, 50, [
                83.69
            ]],
            [51, 100, [
                124.74
            ]],
            [101, 130, [
                161.28
            ]]
        ]]
    ];

for ( var w = 0, w_len = conditions.length; w < w_len; w += 1 )
{
    if ( width >= conditions[w][0] && width <= conditions[w][1] )
    {
        for ( var h = 0, h_len = conditions[w][2].length; h < h_len; h += 1 )
        {
            if ( height >= conditions[w][2][h][0] && height <= conditions[w][2][h][1] )
            {
                price = conditions[w][2][h][2];
                break;
            }
        }
    }
}

检查 Fiddle here .

关于Javascript价格矩阵数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7415730/

相关文章:

javascript - ES6 类多重继承

javascript - 如何在 React 中通过内联样式创建多个回退属性?

c - 如何为二维数组分配和释放堆内存?

c - 并排打印方阵旋转 C

javascript - polymer ,基于对象评估元素

javascript - 查找此表达式的正则表达式(javascript)

c# - 如何使用 C# 在 asp.net 的警告框中显示异常变量值

c - 动态分配的二维数组

c - 在 C 中重新分配双二维数组

c - 算法逻辑实现中关联困难