从JSON中提取数字以使用Javascript进行计算

时间:2012-02-07 19:19:35

标签: javascript jquery json

我有一个计算脚本,可以从JSON获取它的数字,如果他们订购了一定数量的碎片我添加了一些扣除但是当我选择不同的服装时我无法改变它是我的JSON代码:

       var shirtsJSON = [
            {"pattern": "Delta Adult S/S 5.2oz", "basePrice": 5.68, 
                "sizes": {"s-xl": 0, "xxl": 0.84}, 
                "colors": {"white": 0, "athletic": 0.12, "color": 0.23}, 
                "numColors": {"1-2": 0, "3-4": 1.60, "5-6": 3.18, "7-8": 4.81, "9-10": 6.39}, 
                "deductions": {"de48pp": 0, "de72pp": .90, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65},
                "oneLocation": {"onelocnone": 0, "oneloc12": 3.28, "oneloc34": 5.41, "oneloc56": 7.52, "oneloc78": 9.69, "oneloc910": 11.80}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 3.28, "twoloc34": 5.41, "twoloc56": 7.52, "twoloc78": 9.69, "twoloc910": 11.80},
                "threeLocation": {"threelocnone": 0, "threeloc12": 3.28, "threeloc34": 5.41, "threeloc56": 7.52, "threeloc78": 9.69, "threeloc910": 11.80},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 3.28, "fourloc34": 5.41, "fourloc56": 7.52, "fourloc78": 9.69, "fourloc910": 11.80}},

            {"pattern": "Delta Adult S/S 6.1oz", "basePrice": 5.68, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 0, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 3.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Gildan Adult S/S 5.4oz", "basePrice": 1.21, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "onelocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Gildan Adult S/S 6.1oz", "basePrice": 1.31, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Delta Adult 5.5oz Ringspung Tee", "basePrice": 2.48, 
                "sizes": {"s-xl": 0, "xxl": 3}, "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "AIstyle Adult 5.5oz Soft Cotton Tee", "basePrice": 2.40, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Next Level Mens Fitted S/S Tee", "basePrice": 2.76, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Delta Adult 5.2oz L/S Tee", "basePrice": 2.35, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "AIstlye Adult 6oz L/S Tee", "basePrice": 2.70, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Gildan Adult 5.4oz L/S Tee", "basePrice": 3.30, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}}];

然后这是计算的地方

            for (i = 1; i <= numOptions; i++) {
                if ($('#option' + i + 'check').is(':checked')) {
                    totalPrice += parseFloat($('#option' + i + 'check').val());
                }
            }

            for(i = 0; i < shirtsJSON.deductions; i++) {
            }

            /**
            * This adds the final prices into the results area
            */
            $('#48pc').html("48pc : " + currency + getMoney(totalPrice * 48));
            $('#72pc').html("72pc : " + currency + getMoney(totalPrice * 72 ));
            $('#96pc').html("96pc : " + currency + getMoney(totalPrice * 96));
            $('#144pc').html("144pc : " + currency + getMoney(totalPrice * 144));
            $('#288pc').html("288pc : " + currency + getMoney(totalPrice * 288));


            $('#48pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de48pp));
            $('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de72pp));
            $('#96pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de96pp));
            $('#144pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de144pp));
            $('#288pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de288pp));
        }

        //-->

现在它只是绘制了第一种扣除模式,但我想让它随着每件衬衫而改变。我在其中添加了循环,但它仍然只是第一组。

如果您想查看它,这是完整的代码。 https://gist.github.com/1719315

2 个答案:

答案 0 :(得分:1)

它只进行一次计算,因为扣除代码不在for循环中。看看你的花括号。

请注意,变量i的范围是calculatePrices函数。声明for循环标头中的变量不会将该变量范围限定为for循环,而是范围变为for循环的父函数。所以你在几个地方重复使用我。这就是为什么你在for循环之外使用它不会引起错误。

<强>已更新 试试这个(使用sIndex而不是i)

function calculatePrices() {
    totalPrice = 0.00;
    for (var i = 1; i <= numShirts; i++) {
        sIndex = parseInt($('#shirt' + i + 'pattern').val());
        size = $('#shirt' + i + 'size').val();
        color = $('#shirt' + i + 'color').val();
        numColors = $('#shirt' + i + 'numColors').val();
        oneLocation = $('#shirt' + i + 'oneLocation').val();
        twoLocation = $('#shirt' + i + 'twoLocation').val();
        threeLocation = $('#shirt' + i + 'threeLocation').val();
        fourLocation = $('#shirt' + i + 'fourLocation').val();

        price = shirtsJSON[sIndex]["basePrice"];
        price += shirtsJSON[sIndex]["sizes"][size];
        price += shirtsJSON[sIndex]["colors"][color];
        price += shirtsJSON[sIndex]["numColors"][numColors];
        price += shirtsJSON[sIndex]["oneLocation"][oneLocation];
        price += shirtsJSON[sIndex]["twoLocation"][twoLocation];
        price += shirtsJSON[sIndex]["threeLocation"][threeLocation];
        price += shirtsJSON[sIndex]["fourLocation"][fourLocation];

        totalPrice += price;


        /**
        * This goes over the selected options, and adds additional prices if any selected
        * Not sure that using totalPrice for this here is the best strategy. 
        * It works if you have 1 shirt total. But when you add a mechanism to 
        * let users pick a 2nd shirt, it will only take the checked options
        * of the last shirt into account. Maybe add the extra options from
        * each shirt to a global variable, which gets added into the total
        * in calculatePrices.
        */
        for (var j = 1; j <= numOptions; j++) {
            if ($('#option' + j + 'check').is(':checked')) {
                totalPrice += parseFloat($('#option' + j + 'check').val());
            }
        }

        /**
        * This adds the final prices into the results area
        */
        $('#48pc').html("48pc : " + currency + getMoney(totalPrice * 48));
        $('#72pc').html("72pc : " + currency + getMoney(totalPrice * 72 ));
        $('#96pc').html("96pc : " + currency + getMoney(totalPrice * 96));
        $('#144pc').html("144pc : " + currency + getMoney(totalPrice * 144));
        $('#288pc').html("288pc : " + currency + getMoney(totalPrice * 288));


        $('#48pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[sIndex].deductions.de48pp));
        $('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[sIndex].deductions.de72pp));
        $('#96pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[sIndex].deductions.de96pp));
        $('#144pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[sIndex].deductions.de144pp));
        $('#288pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[sIndex].deductions.de288pp));
    }
}

答案 1 :(得分:0)

交换代码:

        for(i = 0; i < shirtsJSON.deductions; i++) {

        }

        $(shirtsJSON).each(function() {

           alert( $(this.deductions).length );

        });

也许这会帮助您指明正确的方向。您正在迭代不正确的对象。使用jQuery的each()函数可能就是你需要查看的那个JS对象。