0

My console.log shows the right values, but I can't access my properties!

var subMenus = [], mainMenu = {};

$.getJSON("js/menu.js", function(data){
  var menuJsonish = data.MenuEXTERNAL;

  //first turn this into an actual array instead of this dumb, non Json stuff
  $.each(menuJsonish, function(e) {
      var name = e.valueOf();
      var eachMenu = [];
      if (e != "MenuSettings") { //skip MenuSettings
          $.each(menuJsonish[name], function(element) {
              var menuArray = [];
              $.each(menuJsonish[name][element], function(item) {
                  var currentItem = menuJsonish[name][element][item];
                  var k = currentItem[0].valueOf();;
                  var v = currentItem[1].valueOf();
                  if (e == 'Menu1') {
                    eval("mainMenu."+k+" = '"+v+"'");
                  } else {
                    eachMenu[k] = v;
                  }
              });
          });
          if (e != 'Menu1') {
              subMenus[e] = eachMenu;
          }
      }
  });
});

// this one returns undefined...
console.log(mainMenu.Menu1);

// yet this one returns all the correct data as shown in the screenshot
console.log(mainMenu);

console.log

I don't understand what is going on here. Shouldn't a value be a value? And more importantly, how do I get my data?

I don't think it's necessary to post the entire "JSON" file (which I did NOT create, but I have to work with), but I will post screenshot #2 which shows what it looks like, and why I have to load it like this.

ugly data

Grokify
  • 15,092
  • 6
  • 60
  • 81
mydoglixu
  • 934
  • 1
  • 7
  • 25

0 Answers0