0

I'm making an ajax call and the response I get is something like this:

   Object {
        Monday noon: Array[4], 
        Tuesday morning: Array[2], 
        Friday noon: Array[3], 
        Sunday: Array[1]
    }

I would like to do something like:

    response.length

But what i get back is undefined

Any idea how can i get the length inside of the Object array?

Another question is: How could I get to the array of Monday noon: Array[4]? Any help or tutorial link will be appreciated.

Louis
  • 1,014
  • 2
  • 11
  • 20

3 Answers3

7

If you want the number of keys in the object use:

var length = Object.keys(obj).length;

If you need a Object.keys polyfill:

if (!('keys' in Object.prototype)) {
  Object.keys = function (obj) {
    var arr = [], prop;
    if (util.toType(obj) === 'object') {
      for (prop in obj) {
        if (obj.hasOwnProperty(prop)) {
          arr.push(prop);
        }
      }
    }
    return arr;
  };
}
Andy
  • 61,948
  • 13
  • 68
  • 95
  • question - could you help me on how to get to the array of `Monday noon: Array[4]` or could you point me to link that could help me with it :/ – Louis Feb 25 '14 at 01:11
  • 1
    @Louis, I can't believe you cancelled the answer. Tch. Post a new question with the structure of the object (not the output in the console) and someone can take a look. – Andy Feb 25 '14 at 01:31
  • yeah, i was going to do that, but every time i do that I start getting negative points because they say i asked already :/ - anyways, thx! – Louis Feb 25 '14 at 01:35
  • @Louis, I ask only because it's not clear whether `monday` is an object and `noon` is an array in that object. If it is you would do `obj.Monday.noon`. – Andy Feb 25 '14 at 01:37
  • the object looks just like this: `{ "Monday noon": [ { "photo": "/media/defaults/profile.png", "meeting": "7:00am", }, { "photo": "/media/defaults/profile.png", "meeting": "1:00pm", } ], "Tuesday morning": [ { "photo": "/media/defaults/profile.png", "meeting": "4:00pm", } ] }` I would like to get to the meeting time... I wonder if that was possible. Sorry still new to this array/js stuff... :/ – Louis Feb 25 '14 at 01:42
  • 1
    `obj['Monday noon']`. You can access object properties with dot notation and by providing the name in square brackets in quotes. Very necessary if you have a space in the property name. – Andy Feb 25 '14 at 01:43
3

Objects don't have a length property. You can loop through the properties and increment a counter:

var counter = 0;
for (var prop in obj) {
  if(obj.hasOwnProperty(prop)){
    counter++;
  }
}
Community
  • 1
  • 1
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • Well, not unless you happen to set a `length` key in the object ;) – gen_Eric Feb 24 '14 at 17:14
  • question - could you help me on how to get to the array of `Monday noon: Array[4]` or could you point me to link that could help me with it :/ – Louis Feb 25 '14 at 01:12
0
var len = 0;
for(var prop in response){
    if(response.hasOwnProperty(prop))
        len++;
}
kamilkp
  • 9,690
  • 6
  • 37
  • 56