I have a screen that needs to calculate prices based on two different parameters, primary (activity in example) and children (ticketType in example)
Previously this was done hackishly, so i decided the best option would be to store the prices in a JSON object populated at runtime, and based on clicks & change events
So i have this json format I formulated, please note you can change this as well, not set in stone
{
"activities": [{
"activitycode": "TK",
"desc": "Tickets",
"ticketTypes": [{
"ticketcode": "G",
"baseprice": 79
}, {
"ticketcode": "P",
"baseprice": 109
}]
}, {
"activitycode": "PK",
"desc": "Parking",
"ticketTypes": [{
"ticketcode": "BK",
"baseprice": 65
}, {
"ticketcode": "ESC-I",
"baseprice": 40
}]
}], //end activities
"handlingPercent": "0.03",
"shipping": "15"
}
So, I tried traverse the JSON object for the correct price, like this
pricing.activities.activitycode['TK'].ticketTypes.ticketcode['G'].baseprice
but this doesnt work, because I havent specified which numerical node to go to yet before supplying my values
Once i did that, the following works
pricing.activities[0].ticketTypes[0].baseprice
But what i would like to accomplish is supplying string/text values, traverse parent nodes, then child nodes, and find the value
I suppose i could do an $.each iteration like this, but i wanted to know what the experts thought first, if this is indeed the best way to go about things
$.each(pricing.activities, function(arrayID, activityData) {
if(activityData.activitycode=="TK")
$.each(activityData.ticketTypes, function(ticketTypeID, typeData) {
if(typeData.ticketcode=="G")
alert(typeData.baseprice);
});
});