Javascript length of an array in an object

I have a Javascript object defined as follows:

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

I am running an array script with:

var myScenario = {
    id:     terrainId,
    text:   text

}; 

active.scenario.push(myScenario);       

However, I get 0 when:

console.log(active.scenario.length);

So of course, I cannot iterate over the contents of the array. If I do this:

console.log(active.scenario)

I see the contents of the array in Chrome plus the correct length of the array. I have a similar code that defines and works with arrays, but outside the definition of an object.

Very grateful for understanding why the length is 0.

+4
source share
1 answer

It works great:

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 elsewhere.

+3
source

Source: https://habr.com/ru/post/1533050/


All Articles