javascript: not getting proper results with callback function


This question already has an answer here:


In Javascript, generic objects do not have a length property.

Common facts about Javascript objects:

  • Objects contain key: value pairs where the keys are strings and values may be any type.
  • Objects do not have a .keys() method to get all of the keys, except...
  • Ecmascript 5 provides an Object.keys() which can be called explicitly as do some 3rd party libs like underscore.js
  • initialized using curly braces {} and key:value pairs

Before that, the usual way to count or access all of the keys unique to a specific instance of an object is with a loop like the following:

for(var k in obj){ 
        if (obj.hasOwnProperty(k)){
          // hasOwnProperty ignores keys from the prototype chain
          // do something with key k, value obj[k]
// the object has l keys specific to this instance

If you need to quickly get a length you should consider using an array object:

  • Array objects (x = [1,2,3,'sam',{'name':'fred', 'status': 'dead'}]) have numeric indices starting with 0 and can contain arbitrary type members.
  • have a length property (x.length is 5)
  • values are accessed by numeric indices with square brackets, i.e. x[2] is 3
  • initialized use square brackets [] containing a comma separated list of values

And as others said in comments, asynchronous calls generally return immediately -- and without the data or object you want in scope. To access the data you must execute code in the context where the data is defined, i.e. write code that accesses the asynchronous data/object in a callback function, not the main code.

Need Your Help

how to open other file manager apps via intent?

android file

in my app i use the code below to open up the downloads and only see PDF files. But it opens up the default Android file manager and for other parts of my app to work i need to use other file manag...