I have a multi-dimensional array, no problem. How do I interrogator one of the arrays in order to ascertain if it actually holds any data? I am working with VS 2008 and what I can see in the debugger is, lets call the element x, is x{...}. However, if I try and use x.length i get the message 'undefined' - so how do I ascertain if the array has nothing in it?
我有一个多维数组,没问题。我如何询问其中一个数组,以确定它是否真的包含任何数据?我正在使用VS 2008,我在调试器中看到的是,让我们调用元素x,是x {...}。但是,如果我尝试使用x.length,我会收到消息'undefined' - 那么如何确定数组中是否包含任何内容?
type(x) is returning an object. Here is the constructor code:
type(x)返回一个对象。这是构造函数代码:
function initArray() {
var length = initArray.arguments.length for (var i = 0; i < length; i++)
{ this[i+1] = initArray.arguments[i]; } }
So it should return a .length value, which it isn't!
所以它应该返回一个.length值,它不是!
In the multilevel array, it's the third level down so x = pub[1][8] which should be the target array, but as I said if I then go x.length, or x[].length I get undefined...
在多级数组中,它是第三级,所以x = pub [1] [8]应该是目标数组,但正如我所说的,如果我然后去x.length,或x []。长度我得到未定义.. 。
Thanks.
6 个解决方案
#1
It looks like your object x is not an array at all.
看起来您的对象x根本不是数组。
If it were, the length would be zero or positive integer.
如果是,则长度为零或正整数。
It sounds like you might have an object and inadvertently added expando properties.
听起来你可能有一个对象并且无意中添加了expando属性。
try this:
for(var prop in x) {
var p = prop; // set a breakpoint on this line and check the value of prop
}
#2
Are you sure it's an array and not an object? Arrays use [] and objects use {}.
你确定它是一个数组而不是一个对象吗?数组使用[],对象使用{}。
The array's length should be zero upon its definition.
数组的长度在定义时应为零。
You can check typeof(x)
to see if it's undefined
.
您可以检查typeof(x)以查看它是否未定义。
This is a case where we're shooting in the dark without seeing your code.
这是我们在没有看到您的代码的情况下在黑暗中拍摄的情况。
#3
check if the object is an array first:
首先检查对象是否为数组:
function isArray(obj) {
return obj.constructor == Array;
}
#4
If it's an array created like "x = new Array();" or "x = [];", then it ought to have a .length property. If it doesn't have a .length property, then you are likely dealing with a null reference (no object) or else you aren't dealing with an array.
如果它是一个像“x = new Array();”那样创建的数组或“x = [];”,那么它应该有一个.length属性。如果它没有.length属性,那么您可能正在处理空引用(没有对象),否则您不处理数组。
#5
You could use a boolean operation to check it as you mentioned.
你可以使用布尔运算来检查它。
var arrayHasValues = false;
if (x.length) { // <-- Will be 'true' if 'length' is not null or undefined
if (x.length > 0) {
arrayHasValues = true;
}
}
if (arrayHasValues) {
// Do Something with Array
}
#6
The only vector data type in JavaScript that lacks a length property is Object
, so you're likely dealing with an object and not an Array.
JavaScript中缺少长度属性的唯一向量数据类型是Object,因此您可能正在处理对象而不是数组。
#1
It looks like your object x is not an array at all.
看起来您的对象x根本不是数组。
If it were, the length would be zero or positive integer.
如果是,则长度为零或正整数。
It sounds like you might have an object and inadvertently added expando properties.
听起来你可能有一个对象并且无意中添加了expando属性。
try this:
for(var prop in x) {
var p = prop; // set a breakpoint on this line and check the value of prop
}
#2
Are you sure it's an array and not an object? Arrays use [] and objects use {}.
你确定它是一个数组而不是一个对象吗?数组使用[],对象使用{}。
The array's length should be zero upon its definition.
数组的长度在定义时应为零。
You can check typeof(x)
to see if it's undefined
.
您可以检查typeof(x)以查看它是否未定义。
This is a case where we're shooting in the dark without seeing your code.
这是我们在没有看到您的代码的情况下在黑暗中拍摄的情况。
#3
check if the object is an array first:
首先检查对象是否为数组:
function isArray(obj) {
return obj.constructor == Array;
}
#4
If it's an array created like "x = new Array();" or "x = [];", then it ought to have a .length property. If it doesn't have a .length property, then you are likely dealing with a null reference (no object) or else you aren't dealing with an array.
如果它是一个像“x = new Array();”那样创建的数组或“x = [];”,那么它应该有一个.length属性。如果它没有.length属性,那么您可能正在处理空引用(没有对象),否则您不处理数组。
#5
You could use a boolean operation to check it as you mentioned.
你可以使用布尔运算来检查它。
var arrayHasValues = false;
if (x.length) { // <-- Will be 'true' if 'length' is not null or undefined
if (x.length > 0) {
arrayHasValues = true;
}
}
if (arrayHasValues) {
// Do Something with Array
}
#6
The only vector data type in JavaScript that lacks a length property is Object
, so you're likely dealing with an object and not an Array.
JavaScript中缺少长度属性的唯一向量数据类型是Object,因此您可能正在处理对象而不是数组。