admin管理员组文章数量:1024659
I have the following Json result:
(Taken from the console)
data
Object {0: Object, 1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 7: Object}
if i in the console do data[0]
i get the following result:
Object {id: 125, Module_id: 2, academy_id: 7, Team_id: 5, end: "2014-08-12 00:00:00"…}
However when i do data.length
the value is undefined
can anyone tell me what is going on?
I have the following Json result:
(Taken from the console)
data
Object {0: Object, 1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 7: Object}
if i in the console do data[0]
i get the following result:
Object {id: 125, Module_id: 2, academy_id: 7, Team_id: 5, end: "2014-08-12 00:00:00"…}
However when i do data.length
the value is undefined
can anyone tell me what is going on?
- 13 data is not an array... it is an object so don't have length property – Arun P Johny Commented Aug 14, 2014 at 12:30
- 1 Change the JSON structure so it is an array of objects. You're trying to get the length of an object which is invalid. – Bobby W Commented Aug 14, 2014 at 12:31
- See this answer: stackoverflow./a/6756305/1232526 – Noy Commented Aug 14, 2014 at 12:32
-
Try this instead
Object.keys(data).length
– Dalorzo Commented Aug 14, 2014 at 12:34
3 Answers
Reset to default 6Use Object.keys
Object.keys(data).length;
DEMO
The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
You structured your json result as an Object
not an Array
. Do not mix those up! data[0]
is the value of the first instance variable of the result object. If you want to structure your json object as an array before sending it to be parsed, then in your php file make sure you encode the object as an array
read.php
<?
$data = array('a' => 1, 'b' => 2, 'c' => 3);
print json_encode($data);
?>
This will work as expected:
Object.getOwnPropertyNames(data).length
I have the following Json result:
(Taken from the console)
data
Object {0: Object, 1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 7: Object}
if i in the console do data[0]
i get the following result:
Object {id: 125, Module_id: 2, academy_id: 7, Team_id: 5, end: "2014-08-12 00:00:00"…}
However when i do data.length
the value is undefined
can anyone tell me what is going on?
I have the following Json result:
(Taken from the console)
data
Object {0: Object, 1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 7: Object}
if i in the console do data[0]
i get the following result:
Object {id: 125, Module_id: 2, academy_id: 7, Team_id: 5, end: "2014-08-12 00:00:00"…}
However when i do data.length
the value is undefined
can anyone tell me what is going on?
- 13 data is not an array... it is an object so don't have length property – Arun P Johny Commented Aug 14, 2014 at 12:30
- 1 Change the JSON structure so it is an array of objects. You're trying to get the length of an object which is invalid. – Bobby W Commented Aug 14, 2014 at 12:31
- See this answer: stackoverflow./a/6756305/1232526 – Noy Commented Aug 14, 2014 at 12:32
-
Try this instead
Object.keys(data).length
– Dalorzo Commented Aug 14, 2014 at 12:34
3 Answers
Reset to default 6Use Object.keys
Object.keys(data).length;
DEMO
The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
You structured your json result as an Object
not an Array
. Do not mix those up! data[0]
is the value of the first instance variable of the result object. If you want to structure your json object as an array before sending it to be parsed, then in your php file make sure you encode the object as an array
read.php
<?
$data = array('a' => 1, 'b' => 2, 'c' => 3);
print json_encode($data);
?>
This will work as expected:
Object.getOwnPropertyNames(data).length
本文标签: jqueryJavaScript Ajax data is array but length is undefinedStack Overflow
版权声明:本文标题:jquery - JavaScript Ajax data is array but length is undefined - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745619312a2159481.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论