Javascript数组返回长度为0,即使有元素也是如此 [英] Javascript array returns length as 0 always even there are elements in it

查看:74
本文介绍了Javascript数组返回长度为0,即使有元素也是如此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的javascript数组,里面有几个元素。当我尝试读取数组的长度时,我总是得到0作为长度。任何人都可以告诉我为什么会这样。

I have a javascript array like below with several elements in it. When I try to read the length of the array I am always getting 0 as the length. Can any one tell me why this is like this.

我的数组是这样的:

var pubs = new Array();
pubs['b41573bb'] =['Albx Swabian Alb Visitor Guide','','15.12.2007 09:32:52',['0afd894252c04e1d00257b6000667b25']];
pubs['6c21a507'] =['CaSH','','29.05.2013 14:03:35',['30500564d44749ff00257b7a004243e6']];


推荐答案

您似乎将数组与对象混淆。你拥有的不是数组。数组只能有整数索引。在你的例子中,你似乎使用了一些 b41573bb 6c21a507 这些不是整数,所以你没有阵列。你有一个带有这些属性的javascript对象。数组看起来像这样:

You seem to be confusing an array with object. What you have is not an array. An array can only have integer indexes. In your example you seem to be using some b41573bb and 6c21a507 which are not integers, so you don't have an array. You have a javascript object with those properties. An array would have looked like this:

var pubs = new Array();
pubs[0] = ['Albx Swabian Alb Visitor Guide','','15.12.2007 09:32:52',['0afd894252c04e1d00257b6000667b25']];
pubs[1] = ['CaSH','','29.05.2013 14:03:35',['30500564d44749ff00257b7a004243e6']];

现在,当您调用.length时,您将获得正确数量的元素(2)。

Now when you call .length you will get the correct number of elements (2).

这篇关于Javascript数组返回长度为0,即使有元素也是如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆