简体   繁体   English

对象中数组的Javascript长度

[英]Javascript length of array in object

I have a Javascript object defined as follows: 我有一个Javascript对象定义如下:

var active = {
    waypoints: [],
    scenario: []
}

I push to array scenario with: 我推送到阵列场景:

var myScenario = {
    id:     terrainId,
    text:   text

}; 

active.scenario.push(myScenario);       

However I get 0 when: 但是我得到0时:

console.log(active.scenario.length);

So of course I cannot loop through the array content. 所以当然我无法遍历数组内容。 If I do: 如果我做:

console.log(active.scenario)

I see the array content within Chrome plus the correct array length. 我看到Chrome中的数组内容加上正确的数组长度。 I have similar code that defines and works with arrays, but outside of an object definition. 我有类似的代码定义和使用数组,但在对象定义之外。

Most grateful for insight into why length is 0. 非常感谢有关长度为0的原因。

It works fine: 它工作正常:

JSFiddle 的jsfiddle

var terrainId = 1;
var text = "text";

var active = {
    waypoints: [],
    scenario: []
}

var myScenario = {
    id:     terrainId,
    text:   text
}; 

active.scenario.push(myScenario);       

console.log(active.scenario.length);

Looks like the problem is somewhere else. 看起来问题出在其他地方。

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

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