var myCategory; If (eating cheese) myCategory = data.cheese; If (eating bread) myCategory = data.bread; ... $.each(myCategory, function(i, item) { do a ton of stuff }
Another option is to use eval(), but that command is evil. :) myType = "cheese"; eval("myCategory = data." + myType + ";"); // untested $.each(myCategory, function(i, item) { do a ton of stuff } On Sep 11, 1:10 pm, cbandes <[EMAIL PROTECTED]> wrote: > Hi - I have a function which iterates through a json file in order to > build an FAQ page. Within the json are several different categories, > and I want to make a generic function that will return only the > appropriate items from the chosen category. > > Currently it looks like this: > > $.getJSON("faq_json.js", function(data) > { > $.each(data.cheese, function(i, item) > { do a ton of stuff > } > > But let's say I want to display stuff that isn't in data.cheese - like > the stuff from data.meat or data.bread - currently I would need to > hardcode those in. > > What I had hoped to do was this: > > var myCategory = "cheese"; > $.each(data.myCategory, function(i, item) > { do a ton of stuff > } > > But that doesn't work. I'm sure there's a simple fix, but I have no > idea where to look... (Yeah, I'm a n3wB ;) thanks for your patience!)