简体   繁体   中英

Printing objects from a JSON file to the console in Javascript

I have got a bit of a basic question, but since I'm still a student I can't seem to figure it out.

I have got the following method:

getMenus: function () {
   var self=this;
   $.getJSON('../data/voordeelmenus.json',function(data){
      self.menus=$.map(data, function(item, i){
         return new Menu(item.id,item.naam,item.type,item.omschrijving,item.samenstelling,item.prijs);    
     });
      self.menusToHtml(); 
   });

The method should (if my code is correct) get menus from a JSON file and make objects of them using the Menu constructor. The objects get stored in the menus array. As you can see I then call to menusToHtml method, but I actually want to test if the objects got stored in the array by printing them out in the console first. I have tried doing this by writing console.log(self.menus) but it doesn't seem to work.

Try JSON.stringify on the object first. That should work.

console.log(JSON.stringify(self.menus));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM