I have a calculation form which gets its values from a 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": .9, "de96pp": 1.35, "de144pp": 2.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": 6.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": .70, "de96pp": 1.25, "de144pp": 2.47, "de288pp": 2.55},
"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}},
Then I have the final results here
$('#48pp').html("Per Piece : " + currency + getMoney(totalPrice));
$('#72pp').html("Per Piece : " + currency + getMoney(totalPrice));
$('#96pp').html("Per Piece : " + currency + getMoney(totalPrice));
$('#144pp').html("Per Piece : " + currency + getMoney(totalPrice));
$('#288pp').html("Per Piece : " + currency + getMoney(totalPrice));
I want it to subtract the values from the deductions JSON I tried doing this:
$('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON.deductions[0].de72pp));
but it didn’t work and I can’t seem to figure it out. Can anyone help?
I think the [0] is in the wrong place – try it after
shirtsJSONinstead ofdeductions:instead of:
This is because
shirtsJSONis an array, whereasdeductionsnot.EDIT:
Quick example of a loop to retrieve multiple
deductions. This assumes that eachdeductionspart contains the same pieces of information each time (e.g. they all contain48pp,72ppetc.)Notice how the
ihas replaced the0inshirtsJSON[0]. You would need to change your code slightly so that it.appended (which adds to the end of whatever’s in that element), instead of using.html(which overwrites whatever’s in that element).EDIT 2:
Here’s a link that you might find helpful: http://blog.xkoder.com/2008/07/10/javascript-associative-arrays-demystified/