0

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?

3 Answers 3

1

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']
Sign up to request clarification or add additional context in comments.

1 Comment

I will for sure! I got to wait 10 more mins stackoverflow says ;)
1

You aren't using an object, you are using an array.

An object looks like this:

var obj = {
    id: 0
};

What you have is an object within an array: (note [)

var obj = [{id: 0}];

Comments

1

Obj is an array with single element as object, hence to access position you can use any of these syntax

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

Again cartesian is an array hence to call push you can use

    Obj[0]['position']['cartesian'].push(foobar[i][t][p])
    Obj[0].position.cartesian.push(foobar[i][t][p])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.