Javascript根据键从对象中获取值,并与其大小相比,小于下一个键

标签 javascript

尝试根据包裹的重量计算运费。

我有一个包含给定重量价格的对象,例如

shippingPrices = {
    'economy': {
         [weight]: price,
         . . . 
     }
};

然后我想通过传递包裹重量的函数来获得正确的运费,例如:

addShippingPrice(700);  // 700 is weight in grams

我试过这样:

shippingPrices = {
    'economy': {
        2000: 7,
        5000: 9,
        10000: 10,
        20000: 15,
        30000: 22
    }
};

var addShippingPrice = function(weight) {
    var price = 0;
    for( var maxWeight in shippingPrices.economy) {
        maxWeight = parseInt(maxWeight, 10);
        
        console.log(shippingPrices.economy[maxWeight]);
      
        if(weight < maxWeight) {
            // return price = shippingPrices.economy[maxWeight];
        }
    }
    console.log('amount', price);
};

addShippingPrice(700);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

在这种情况下,“正确”的运费为 7,如果我调用重量为 8000 的函数 addShippingPrice(8000); 它应该返回 10

我怎样才能实现这一目标?另外,如果将 ShippingPrices 对象更改为数组会更好,我会更改它!

最佳答案

您可以使用可迭代的结构,例如

var shippingPrices = {
    economy: [
        { weight: 2000, price: 7 },
        { weight: 5000, price: 9 },
        { weight: 10000, price: 10 },
        { weight: 20000, price: 15 },
        { weight: 30000, price: 22 }
    ],
    priority: [
        { weight: 2000, price: 9 },
        { weight: 5000, price: 11 },
        { weight: 10000, price: 12 },
        { weight: 20000, price: 18 },
        { weight: 30000, price: 25 }
    ]
};

function getShippingPrice(type, weight) {
    var price;
    shippingPrices[type].some(function (a) {
        if (a.weight >= weight) {
            price = a.price;
            return true;
        }
    });
    return price;
}

var shippingPrices = { economy: [{ weight: 2000, price: 7 }, { weight: 5000, price: 9 }, { weight: 10000, price: 10 }, { weight: 20000, price: 15 }, { weight: 30000, price: 22 }], priority: [{ weight: 2000, price: 9 }, { weight: 5000, price: 11 }, { weight: 10000, price: 12 }, { weight: 20000, price: 18 }, { weight: 30000, price: 25 }] };

console.log(getShippingPrice('economy', 700));
console.log(getShippingPrice('economy', 8000));

ES6

function getShippingPrice(type, weight) {
    return (shippingPrices[type].find(a => a.weight >= weight) || {}).price;
}

var shippingPrices = { economy: [{ weight: 2000, price: 7 }, { weight: 5000, price: 9 }, { weight: 10000, price: 10 }, { weight: 20000, price: 15 }, { weight: 30000, price: 22 }], priority: [{ weight: 2000, price: 9 }, { weight: 5000, price: 11 }, { weight: 10000, price: 12 }, { weight: 20000, price: 18 }, { weight: 30000, price: 25 }] };

console.log(getShippingPrice('economy', 700));
console.log(getShippingPrice('economy', 8000));

关于Javascript根据键从对象中获取值,并与其大小相比,小于下一个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39545198/

相关文章:

javascript - 使用document.getElementById方法时for循环的Javascript不起作用

javascript - Mongoose : "Invalid schema configuration : ' model' is not a valid type within the array"

如果文本字段为空,则在提交时发出 Javascript 警报

javascript - 无法将数据从 Controller 读取到 View 中

javascript - 有没有 "vqmod"允许多图片上传?

JavaScript 不是从外部链接加载的

javascript - javascript 中未定义错误函数

javascript - 如何将访问 token 从 .NET(代码隐藏)传递到 Javascript

javascript - OSX Safari/quicktime 不发送带有媒体文件请求的 cookie

javascript - 如何确定参数是否提供给 Javascript 函数?