admin管理员组文章数量:1023803
I have a PHP function that returns a multiple-level JSON object in the format
var data = {
1 : {
1: { Object },
2: { Object },
...,
...
},
2: {
1: { Object },
2: { Object },
...,
...
}
}
I need to get the length of data
to initialize a for loop maximum value. By this, I mean the count of the first level entries.
data[1].length
returns 22, and data[2].length
returns 23 via the console.log
printout. This is expected, but data.length
returns undefined.
Is there a better method than
var count = 0;
for (var i=1; i< Number.MAX_VALUE; i++){
if (typeof data[i] != 'undefined')
count = i;
}
I have a PHP function that returns a multiple-level JSON object in the format
var data = {
1 : {
1: { Object },
2: { Object },
...,
...
},
2: {
1: { Object },
2: { Object },
...,
...
}
}
I need to get the length of data
to initialize a for loop maximum value. By this, I mean the count of the first level entries.
data[1].length
returns 22, and data[2].length
returns 23 via the console.log
printout. This is expected, but data.length
returns undefined.
Is there a better method than
var count = 0;
for (var i=1; i< Number.MAX_VALUE; i++){
if (typeof data[i] != 'undefined')
count = i;
}
Share
Improve this question
asked Aug 30, 2012 at 16:30
JasonJason
11.4k22 gold badges93 silver badges185 bronze badges
2
- is there any way you can store your data in an array of objects instead of an object? – dm03514 Commented Aug 30, 2012 at 16:33
- I really don't have control over that part at this time, unfortunately. – Jason Commented Aug 30, 2012 at 16:38
4 Answers
Reset to default 3How About
Object.keys(data).length
If you can control the JSON format, nested arrays seem a better alternative.
If not, you can run something like
var length = 0;
for (var i in data) {
if (isFinite(i) && i > length)
length = i;
}
You can count the number of entries in a loop, like this:
var length = 0;
for (var key in data) {
if (data.hasOwnProperty(key)) {
length++;
}
}
However, if you have control over the returned data, it would probably be easier to return an array instead of an object because the array will already have the length property you want.
Why not just do this instead of assigning numeric properties? Arrays will have the length property you wish:
var data = [
[
{ Object },
{ Object },
...,
...
],
[
{ Object },
{ Object },
...,
...
]
]
I have a PHP function that returns a multiple-level JSON object in the format
var data = {
1 : {
1: { Object },
2: { Object },
...,
...
},
2: {
1: { Object },
2: { Object },
...,
...
}
}
I need to get the length of data
to initialize a for loop maximum value. By this, I mean the count of the first level entries.
data[1].length
returns 22, and data[2].length
returns 23 via the console.log
printout. This is expected, but data.length
returns undefined.
Is there a better method than
var count = 0;
for (var i=1; i< Number.MAX_VALUE; i++){
if (typeof data[i] != 'undefined')
count = i;
}
I have a PHP function that returns a multiple-level JSON object in the format
var data = {
1 : {
1: { Object },
2: { Object },
...,
...
},
2: {
1: { Object },
2: { Object },
...,
...
}
}
I need to get the length of data
to initialize a for loop maximum value. By this, I mean the count of the first level entries.
data[1].length
returns 22, and data[2].length
returns 23 via the console.log
printout. This is expected, but data.length
returns undefined.
Is there a better method than
var count = 0;
for (var i=1; i< Number.MAX_VALUE; i++){
if (typeof data[i] != 'undefined')
count = i;
}
Share
Improve this question
asked Aug 30, 2012 at 16:30
JasonJason
11.4k22 gold badges93 silver badges185 bronze badges
2
- is there any way you can store your data in an array of objects instead of an object? – dm03514 Commented Aug 30, 2012 at 16:33
- I really don't have control over that part at this time, unfortunately. – Jason Commented Aug 30, 2012 at 16:38
4 Answers
Reset to default 3How About
Object.keys(data).length
If you can control the JSON format, nested arrays seem a better alternative.
If not, you can run something like
var length = 0;
for (var i in data) {
if (isFinite(i) && i > length)
length = i;
}
You can count the number of entries in a loop, like this:
var length = 0;
for (var key in data) {
if (data.hasOwnProperty(key)) {
length++;
}
}
However, if you have control over the returned data, it would probably be easier to return an array instead of an object because the array will already have the length property you want.
Why not just do this instead of assigning numeric properties? Arrays will have the length property you wish:
var data = [
[
{ Object },
{ Object },
...,
...
],
[
{ Object },
{ Object },
...,
...
]
]
本文标签: javascriptGet number of rows of multidimensional JSON variableStack Overflow
版权声明:本文标题:javascript - Get number of rows of multidimensional JSON variable - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745560840a2156154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论