-3

I'm grabbing a list of elements ids thusly.

var menus = $(".menu").map(function(){
            return this.id;
        });

Which returns something like:

["lunch", "appetizers", "soup", "salads", "seafood", "noodles", "stir_fry", "curry", "kids", "steak", "dessert", "sides"]

For each item in the array I want to grab some JSON data.

        $.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);
        });

Such that data.menu.list would be data.menu.lunch, data.menu.appetizers, etc.

The JSON is structured like so:

{
    "menu": {
        "lunch": [{
            "name": "Kao PAdd",
            "desc": "Fried rice with onions, green onions, snow peas, and egg / Chicken, vegetarian / Shrimp or tofu (Add $1)"
        }

Any thoughts that don't involve eval()?

EDIT: I when I do this:

$.each(data.menu,function(i) {
    console.log(data.menu[i].key);
});

the console gives me:

Object {lunch: Array(14), appetizer: Array(11)}

All I really want is to access those arrays.

console.log(data.menu[i].name)

gives me a pair of undefineds.

5 Answers5

1

If you're looking for parsing JSON string to object here you go:

var jsonString = '{"data":{"item":{"id":1,"value":"foo"}}}';
var jsonObj = JSON.parse(jsonString);
console.log(jsonObj.data.item.value);
nocodename
  • 1,246
  • 9
  • 15
1

That’s a brilliant question, Sir!

No matter how you retrieve your menus, strToVar() will do the task.
This code converts strings from array to variable names:

Solution:
    var strToVar = (str,val) => this[str] = val;

Example:
    var menus = ["lunch", "appetizers", "soup", "salads", "seafood", "noodles", 
        "stir_fry", "curry", "kids", "steak", "dessert", "sides"];
    menus.forEach(strToVar);
    prompt("[lunch, appetizers, soup, salads, seafood, noodles, " +
        "stir_fry, curry, kids, steak, dessert, sides]", 
        [lunch, appetizers, soup, salads, seafood, noodles, 
        stir_fry, curry, kids, steak, dessert, sides]);

Give me all your points.

Community
  • 1
  • 1
7vujy0f0hy
  • 8,741
  • 1
  • 28
  • 33
1

The problem was, I didn't understand what I really wanted to do. I asked the wrong question (although it's an interesting one, so I'll leave it up).

I thought I needed to generate my variable list from the HTML ids, but that was a mistake. What I needed was simply another for loop (or jQuery each());

    $.each(data.menu, function(i) {
        var list = data.menu[i],
            menus = [];
        $.each(list, function(x) {
            var items = '<li><h3>' + list[x].name + '</h3><p>' + list[x].desc + '</p></li>';

            menus.push(items)
        });
        $('#' + i).append(menus);
    });
0

Your $.each function should be:

$.each(menus, function(i, list) { // the second parameter is list so we don't need menus[i]
    var meal = data.menu[list],   // use of bracket notation
        items = '<li><h3>' + meal.name + '</h3><p>' + meal.desc + '</p></li>';

    $('#' + list).append(items);
    //        ^^^ no need for the ".menu" as IDs alone are sufficient (assuming you have unique IDs, otherwise you have a problem)
});

Docs on MDN for bracket notation.

ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
0

As per my understanding you want to achieve something like this :

var menus = ["lunch", "appetizers", "soup", "salads"];
var menuList = [
                 {
                   "name":"lunch",
                   "description":"description1"
                 },
                 {
                   "name":"appetizers",
                   "description":"description2"                 
                 },
                 {
                   "name":"soup",
                   "description":"description3"                 
                 },
                 {
                   "name":"salads",
                   "description":"description4"                 
                 }
               ]
var menu = {};

for(var i in menus) {
  menu[menus[i]] = [{
      "name": menuList[i].name,
      "desc": menuList[i].description
  }];
}

console.log(menu);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123