简体   繁体   English

元素数组长度为0

[英]Array with elements has length = 0

I am making an application on javascript, the problem is that I have an object containing an array with elements, but when I try to get one of them it behaves as if the array was empty. 我正在使用javascript编写应用程序,问题是我有一个包含包含元素数组的对象,但是当我尝试获取其中一个元素时,它的行为就好像该数组为空。

The way I am populating my array is very simple: 我填充数组的方式非常简单:

object.array = new Array();
for(element in resultFromDatabase){
    object.array.push(element);
}
console.debug(object);
console.debug(object['array']);

I made a console.debug() of the object and in the next line a console.debug() of the array within the object. 我创建了对象的console.debug(),并在下一行中创建了对象内数组的console.debug()。

The first one shows the elements of the object including the array I need, with three elements inside of it. 第一个显示对象的元素,包括我需要的数组,其中包含三个元素。 The second one shows an empty array. 第二个显示一个空数组。

Do you have any idea what could this be? 你有什么想法吗?

Your question is quite vague, but I'm going to make an assumption. 您的问题很模糊,但是我要作一个假设。

for (element in resultFromDatabase) {
}

"element" in the above example is the actual enumerator (1, 2, 3, 4, 5, etc). 在上面的示例中,“元素”是实际的枚举数(1、2、3、4、5等)。 It won't contain any objects from resultFromDatabase. 它不会包含resultFromDatabase中的任何对象。

The expected result from your example is an array looking something like... 您的示例的预期结果是一个看起来像...的数组

["1", "2", "3", "4"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM