数组长度返回0

贾斯汀·哈里斯(Justin Harris)

谁能帮助解释为什么length返回0?非常感谢您!

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

因为这不是您添加项目的方式,因为数组仅接受数字键。您可以这样操作:

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

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章