Gru*_*ard 5 javascript arrays object
我有一个 Javascript 对象定义如下:
var active = {
waypoints: [],
scenario: []
}
Run Code Online (Sandbox Code Playgroud)
我推送到数组场景:
var myScenario = {
id: terrainId,
text: text
};
active.scenario.push(myScenario);
Run Code Online (Sandbox Code Playgroud)
但是我在以下情况下得到 0:
console.log(active.scenario.length);
Run Code Online (Sandbox Code Playgroud)
所以当然我不能遍历数组内容。如果我做:
console.log(active.scenario)
Run Code Online (Sandbox Code Playgroud)
我看到 Chrome 中的数组内容加上正确的数组长度。我有类似的代码来定义和使用数组,但在对象定义之外。
非常感谢您深入了解为什么长度为 0。
它工作正常:
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);
Run Code Online (Sandbox Code Playgroud)
看起来问题出在其他地方。