hunterInt hunterInt - 3 months ago 7
Javascript Question

javascript access multidimensional object not working

I have an object:

var Obj = [{
id: "",
position: {
cartesian: [],
polar: [],
bob: "INERTIAL"
}
}];


When I go to set obt.id

Obj.id="sam/reconnaissance - "+samName;


It works fine.

However when I go to access Obj.position.cartesian

(Obj.position.cartesian).push(fooBar[i][t][p]);


I get this error:

(Obj.position.cartesian).push(fooBar[i][t][p]);
^
TypeError: Cannot read property 'cartesian' of undefined


Why? I have tried
Obj['position']['cartesian']
and still nothing

What am I doing wrong?

Answer

Obj is not an object, it is an Array so you have to access first element with index 0. Try it like

Obj[0]['position']['cartesian']