【问题标题】:Jquery - find value in array having 4 variablesJquery - 在具有 4 个变量的数组中查找值
【发布时间】:2016-04-27 11:30:23
【问题描述】:

我想创建包装费计算器。 我创建了几个滑块。有高度、宽度、长度和重量滑块。用户设置所有滑块后,我想从数组中检索费用。 最好的方法是什么?

链接到带有数据的表:https://services.amazon.co.uk/services/fulfilment-by-amazon/pricing.html

链接到我的工具:http://tests.uk4u.pl/amazonProfitCalc/

我做了什么:

var slider_length_value = $(".slider-length").slider("value");
var slider_width_value = $(".slider-width").slider("value");
var slider_height_value = $(".slider-height").slider("value");

var slider_weight_value = $(".slider-weight").slider("value");   

var fullfilmentFee = Array({
        "packaging": "Small Envelope",
        "length": "20",
        "width": "15",
        "height": "1",
        "weight": "100",
        "fee": "1.07"
    },

    {
        "packaging": "Standard Envelope",
        "length": "33",
        "width": "23",
        "height": "2.5",
        "weight": "100",
        "fee": "1.19"
    }, {
        "packaging": "Standard Envelope",
        "length": "33",
        "width": "23",
        "height": "2.5",
        "weight": "250",
        "fee": "1.31"
    }, {
        "packaging": "Standard Envelope",
        "length": "33",
        "width": "23",
        "height": "2.5",
        "weight": "500",
        "fee": "1.51"
    },

    {
        "packaging": "Large Envelope",
        "length": "33",
        "width": "23",
        "height": "5",
        "weight": "1000",
        "fee": "1.70"
    },

    {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "250",
        "fee": "1.66"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "500",
        "fee": "1.72"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "1000",
        "fee": "1.77"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "1500",
        "fee": "2.19"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "2000",
        "fee": "2.41"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "3000",
        "fee": "3.30"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "4000",
        "fee": "3.40"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "5000",
        "fee": "3.40"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "6000",
        "fee": "3.45"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "7000",
        "fee": "3.45"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "8000",
        "fee": "3.53"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "9000",
        "fee": "3.53"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "10000",
        "fee": "3.53"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "11000",
        "fee": "3.53"
    }, {
        "packaging": "Standard Parcel",
        "length": "45",
        "width": "34",
        "height": "26",
        "weight": "12000",
        "fee": "3.54"
    }
);


var fee = $.map(fullfilmentFee, function(value, key) {
    if (value.length == "33") {
        //return value.fee;
        return value.fee;

    } else {
        $(".message").html("Error!!!");
    }
});
//console.log(price);
//output: $220
$(".message").html(fee);

编辑:

也许再一次。用户准备包装并使用滑块设置此包装的长度宽度高度和重量。接下来,他按下提交按钮并获得与这些尺寸和重量相对应的履行费用。如果所有的暗淡都与数组中的完全一样,那就很容易了,但是:

  1. 如何在数组中的数字之间创建条件,例如 between

    履行[0].length和履行[1].length

  2. 如果一个或多个变量(长度或宽度或高度或重量)的值高于正常范围,我想获得与该值相对应的费用。

对我的英语表示敬意和抱歉

【问题讨论】:

    标签: jquery arrays search


    【解决方案1】:

    你有一个带有对象的数组,所以我们可以做简单的 for 循环来迭代数组单个对象,我们可以从每个数组对象的 'fee' 键中获取费用金额,如下所示:

    function getCalculatedFee(fullfilmentFee){
            var fullAmount = 0;
    
            for(var i=0; i<=fullfilmentFee.length; i++){
               fullAmount += fullfilmentFee[i].fee;
            }
    
            return fullAmount;
        }
    

    要获取单个值,请尝试以下操作:

    fullfilmentFee[0].fee
    

    根据您的示例,您可以创建 4 个函数,这些函数会按精确值(如长度)收取费用:

    function getFeeByLength(slider_length_value){
        $.each(fullfilmentFee, function(value, key) {
            if (value.length == slider_length_value) {
                return value.fee;
            }
        });
    }
    

    其余用户选择的值也是如此。使用这种函数方式,您可以随时调用您的函数 - 正是您需要匹配值的地方。

    【讨论】:

    • 您好,谢谢,但您不明白我想要实现的目标。
    • 您写道'在用户设置所有滑块后,我想从数组中检索费用。最好的方法是什么?所以在我看来,这是最好的方法。解释你想要什么而不是这个,也许我可以帮助你
    • 也许您的意思是“我应该实时或按按钮提交计算费用?”所以这个问题更多的是用户体验而不是编程。无论如何 - 请解释你的问题
    • 在用户设置重量、高度、宽度和长度后,我想获得与这些设置相对应的费用。
    • 提交后即可。我不想计算 - 我想从数组中获取。波兹拉维亚姆
    【解决方案2】:

    不太确定您要做什么,但您可以访问数组项的变量。你只需要做fullfilmentFee[i].fee。通过执行fullfilmentFee[i],您将在数组中获得i 项,并且您可以通过执行.andTheVariable 访问该项的变量,因此它将是:.packaging.length.width.height.weight.fee

    例子:

    fullfilmentFee[0].fee 返回1.07

    fullfilmentFee[1].fee 返回1.19

    这就是你要找的吗?

    [编辑]

    我会在这里回答。 您必须检查哪个 i 的长度为 19。为此,您需要检查整个数组,对于长度为 19 的数组,获取其费用:

    var feeForLength19; // where we are going to store the fee
    
    // run the whole array
    for(var i=0;i<=fullfilmentFee.length;i++){
        // for the variable that has length 19, we store his fee
        if(fullfilmentFee[i].length == 19){
            feeForLength19 = fullfilmentFee[i].fee;
        }
    }
    

    【讨论】:

    • 例如,如果用户将滑块长度设置为 19,您能告诉我显示费用如何
    • 您好,我编辑了答案以展示您的示例
    • 数组中没有长度 =19,但它是 20。我想要实现的是从长度为 20 的行中收取费用,因为 20 大于 19。
    • 另一个例子 - 如果用户将长度设置为 22,我想从 fullfilmentFee[1].fee 收取费用,因为 22 介于 20 和 33 之间。问候
    • 按照我写的,只要在if语句中使用另一个条件就可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 2017-07-11
    • 2013-09-07
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    相关资源
    最近更新 更多