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.
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;
};
}
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++;
}
}
var len = 0;
for(var prop in response){
if(response.hasOwnProperty(prop))
len++;
}
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.