数组长度返回0 [英] Array Length returns 0

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

问题描述

任何人都可以帮忙解释为什么长度返回0?非常感谢您提前!

Could anyone help explain why length is returning 0? Thank you VERY much in advance!

var errors = [];
errors[''] = "Monday Morning slot already taken. Would you like to replace it?";
console.log(errors);
document.write(errors.length);


推荐答案

因为这不是你添加项目的方式,因为Arrays只接受数字键。你这样做:

Because that is not how you add an item since Arrays only accept numeric keys. You do it like this:

errors.push("blah");  /*or*/  errors[0] = "blah";

//Now if you check the length:
errors.length;  //1

此外,如果您将其用作对象,' ' 也不是有效名称

Also, if you are using it as an Object, '' isn't a valid name either.

*更正:看起来你可以使用(空字符串)作为键。

*Correction: Looks like you can use "" (empty string) as a key.

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

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