简体   繁体   English

获取对象内部数组的长度

[英]Get length of array inside object

i have the following object in javascript: 我在javascript中有以下对象:

objectX which is an objectX是

Object {names : Array[14]}

im trying to get the length of this, but it returns undefined when i use objectX.length? 我试图得到它的长度,但当我使用objectX.length时,它返回未定义?

Im trying it in chrome debugger, and googling, anyone out there quickly let me know plz. 我在chrome调试器中进行了尝试,然后在Google上进行搜索,很快有人通知我。

object.names.length

要么

object['names'].length

From the output it seems that the property name is 'name ' . 从输出看来,属性名称是'name ' See the differences in the console output: 查看控制台输出中的差异:

> console.log({names: []});
Object {names: Array[0]}

> console.log({'names ': []});
Object {names : Array[0]}      // <- this looks like what you have
//           ^ note the space

So you'd have to do: 因此,您必须执行以下操作:

obj['names '].length

I suggest to fix the property name though, so that you can use obj.name.length instead. 我建议修复属性名称,以便可以改用obj.name.length

Fix your object notation: 修正您的对象符号:

var obj = {name:Array(14)};
alert(obj.name.length);

var obj = {name : [1, 2, 3, 4]};
alert(obj.name.length);

Fiddle 小提琴

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

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