0

i have a problem to access to properties of a javascript object. i have an ajax call where i create a object and in the complete part i want to use the object created but i can't access to the properties when i console log the ojbect it is there but cant acces to properties this is my code :

this is inside treatment in the ajax call i have a function who create an object

var repartion = getEventsByCity(villes,date);

in the ajax complete i even try with .done :

console.log(repartion) 

it print

Object {}
 75001: Array(4)
          0: Object 
          1: Object
          2: Object
          3: Object 
            length: 4
          __proto__: Array(0)
          __proto__: Object

so the object is define but if after i try

console.log(repartion['75001']);

it print undefined if i try

$.each(repartion, function(index,value){
     console.log(index + "  " + value);
});

it print nothing... i don't understand it can someone help me to find what im doing wrong i can't figured out.

thanks.

Update: i show more of my code

this is my ajax call

function callApi(data){
    $.ajax({
        url: 'events_finder.js',
        data: data,
        success: function(arg){ console.log('success ');},
        complete: function(arg){ 
            console.log('complete ');
            // this one working and print the object
            console.log(repartion);
            // not showing anything
            $.each(repartion, function(index,value){
                console.dir(index + "  " + value);
            });
           // showing undefined
            console.log(repartion['75001']);
        },
        error: function(xhr,status,erreur){ console.log(xhr.status+" "+status+" "+ erreur);},
      }).done(function(){
        console.log("DONE");
        // this one working and print the object
            console.log(repartion);
            // not showing anything
            $.each(repartion, function(index,value){
                console.dir(index + "  " + value);
            });
           // showing undefined
            console.log(repartion['75001']);
      })
  } 

this events_finder.js

var villes = data['villes'];
var date = data['date'];
// i put only one city for the debug
villes = ['75001']

function getEventsByCity (villes,date){
  var repartion_events = {};
  for(i = 0 ; i < villes.length ; i++){
    var oArgs = {
      app_key: "fzgree4546Rx" ,
      where: "France, "+villes[i],
      date: date, 
      sort_order: "popularity",
      page_size: "400",
    };
    //console.log(oArgs);

    EVDB.API.call("/events/search", oArgs, function(oData) {
     //console.log(oData);
      // console.log(typeof oData['events']['event']);
      if(oData['events'] != null){
        if (oData['events']['event'] != null){
          var kikou = oData['events']['event'];
          //console.log(kikou);
          var eventsByCity = new Array;
          $.each(kikou, function( index, value ) {
            var events = {
              "description": kikou[index]['description'],
              "image": kikou[index]['image'], 
              "latitude": kikou[index]['latitude'],
              "longitude": kikou[index]['longitude'],
              "postal_code": kikou[index]['postal_code'],
              "start_time": kikou[index]['start_time'],
              "stop_time": kikou[index]['stop_time'],
              "title": kikou[index]['title'],
              "url": kikou[index]['url'],
              "venue_address": kikou[index]['venue_address'],
              "venue_name": kikou[index]['venue_name'],
              "venue_url": kikou[index]['venue_url']
            };
            // rajouter l'objet dans le tableau de la ville 
            eventsByCity.push(events);   
            ville = kikou[index]['postal_code'];  
          }); 
            // console.log(eventsByCity);
            // console.log(ville);

            repartion_events[ville] = eventsByCity;        

        }
      }
    }); 
  }
return repartion_events;
}
var repartion = getEventsByCity(villes,date);
// print the object this working
console.log(repartion repartion['75001'] );
// this one show undefined
console.log(repartion['75001']);
  • try `console.log(repartion[75001]);` – Iceman Apr 20 '17 at 09:36
  • Did you try an integer key like this `console.log(repartion[75001]);` ? – dloeda Apr 20 '17 at 09:36
  • 1
    I'm guessing this has [something to do with the Ajax call](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). You'll have to show a [mcve]. – JJJ Apr 20 '17 at 09:39
  • yes i tried repartion[75001] not working and i even try to change the object into an array and tried repartion[0] or loop not working still undefined... – Tom Fournier Apr 20 '17 at 09:40
  • Are you accessing the array from within the `done` function of the AJAX. – Dan Philip Bejoy Apr 20 '17 at 09:45
  • Yes, this is a combination of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) and the fact that [Chrome's console shows the object value at the time when you expand it](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays), not when you log it. – JJJ Apr 20 '17 at 09:59

0 Answers0