Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript length of array in object

I have a Javascript object defined as follows:

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:

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

like image 272
Grumpybeard Avatar asked Oct 20 '22 11:10

Grumpybeard


1 Answers

It works fine:

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.

like image 58
sanchez Avatar answered Oct 23 '22 01:10

sanchez