Possible Duplicate:
Length of Javascript Associative Array可能重复:Javascript关联数组的长度
I want to check the length of a multidimensional array but I get "undefined" as the return. I'm assuming that I am doing something wrong with my code but I can't see anything odd about it.
我想检查一个多维数组的长度,但我得到“未定义”作为返回。我假设我的代码出错了,但我看不出有什么奇怪的。
alert(patientsData.length); //undefined
alert(patientsData["XXXXX"].length); //undefined
alert(patientsData["XXXXX"]['firstName']); //a name
fruits = ["Banana", "Orange", "Apple", "Mango"];
alert(fruits.length); //4
Thoughts? Could this have something to do with scope? The array is declared and set outside of the function. Could this have something to do with JSON? I created the array from an eval() statement. Why does the dummy array work just fine?
思考?这可能与范围有关吗?声明数组并在函数外部设置。这可能与JSON有关吗?我从eval()语句创建了数组。为什么虚拟阵列工作得很好?
3 个解决方案
#1
9
Those are not arrays. They're objects, or at least they're being treated like objects. Even if they are Array instances, in other words, the "length" only tracks the largest numeric-indexed property.
那些不是数组。它们是物体,或者至少它们被视为物体。即使它们是数组实例,换句话说,“长度”仅跟踪最大的数字索引属性。
JavaScript doesn't really have an "associative array" type.
JavaScript实际上没有“关联数组”类型。
You can count the number of properties in an object instance with something like this:
您可以使用以下内容计算对象实例中的属性数:
function numProps(obj) {
var c = 0;
for (var key in obj) {
if (obj.hasOwnProperty(key)) ++c;
}
return c;
}
Things get somewhat messy when you've got inheritance chains etc, and you have to work out what you want the semantics of that to be based on your own architecture.
当你有继承链等时,事情变得有些混乱,你必须根据你自己的架构找出你想要的语义。
#2
2
.length
only works on arrays. It does not work on associative arrays / objects.
.length仅适用于数组。它不适用于关联数组/对象。
patientsData["XXXXX"]
is not an array. It's a object. Here's a simple example of your problem:
patientsData [“XXXXX”]不是数组。这是一个对象。这是一个简单的问题示例:
var data = {firstName: 'a name'};
alert(data.length); //undefined
#3
0
It appears that you are not using nested array, but are using objects nested within objects because you're accessing members by their names (rather than indexes).
您似乎没有使用嵌套数组,而是使用嵌套在对象中的对象,因为您按名称(而不是索引)访问成员。
#1
9
Those are not arrays. They're objects, or at least they're being treated like objects. Even if they are Array instances, in other words, the "length" only tracks the largest numeric-indexed property.
那些不是数组。它们是物体,或者至少它们被视为物体。即使它们是数组实例,换句话说,“长度”仅跟踪最大的数字索引属性。
JavaScript doesn't really have an "associative array" type.
JavaScript实际上没有“关联数组”类型。
You can count the number of properties in an object instance with something like this:
您可以使用以下内容计算对象实例中的属性数:
function numProps(obj) {
var c = 0;
for (var key in obj) {
if (obj.hasOwnProperty(key)) ++c;
}
return c;
}
Things get somewhat messy when you've got inheritance chains etc, and you have to work out what you want the semantics of that to be based on your own architecture.
当你有继承链等时,事情变得有些混乱,你必须根据你自己的架构找出你想要的语义。
#2
2
.length
only works on arrays. It does not work on associative arrays / objects.
.length仅适用于数组。它不适用于关联数组/对象。
patientsData["XXXXX"]
is not an array. It's a object. Here's a simple example of your problem:
patientsData [“XXXXX”]不是数组。这是一个对象。这是一个简单的问题示例:
var data = {firstName: 'a name'};
alert(data.length); //undefined
#3
0
It appears that you are not using nested array, but are using objects nested within objects because you're accessing members by their names (rather than indexes).
您似乎没有使用嵌套数组,而是使用嵌套在对象中的对象,因为您按名称(而不是索引)访问成员。