Assembling a collection of element IDs like this:
var menus = $(".menu").map(function(){
return this.id;
});
The result might look something like this:
["lunch", "appetizers", "soup", "salads", "seafood", "noodles", "stir_fry", "curry", "kids", "steak", "dessert", "sides"]
My goal is to retrieve JSON data for each item in the array.
$.each(menus,function(i) {
var list = menus[i],
meal = data.menu.list,
items = '<li><h3>' + meal.name + '</h3><p>' + meal.desc + '</p></li>';
$('#'+list+".menu").append(items);
});
In this scenario, data.menu.list
represents data.menu.lunch
, data.menu.appetizers
, and so forth.
The JSON structure is as follows:
{
"menu": {
"lunch": [{
"name": "Kao PAdd",
"desc": "Fried rice with onions, green onions, snow peas, and egg / Chicken, vegetarian / Shrimp or tofu (Add $1)"
}
Any ideas on how to proceed without resorting to eval()?
EDIT: When I execute this code:
$.each(data.menu,function(i) {
console.log(data.menu[i].key);
});
I receive the following output in the console:
Object {lunch: Array(14), appetizer: Array(11)}
All I want to do is access those arrays.
console.log(data.menu[i].name)
This returns a pair of undefined values.