Cam
Cam

Reputation: 1902

Object returned is Undefined

I have an list of Objects and I am trying to dynamically create additional sub objects. The task works, but I am getting an Undefined result for the parent. It should put my content into the Data creating a sub object. I figure I am missing a step in the process which is something to do with converting a String to an object.

var steps_array = 
{
    Step_1:
    {
        Data : ""

    },
    Step_2:
    {
        Data : ""
    },
    Step_3:
    {
        Data : ""
    },
    Step_4:
    {
        Data : ""
    }
}

Javascript to load dynamic content

steps_array.Step_3[steps_array.Data] = {loc:{location : 4}};

Upvotes: 0

Views: 44

Answers (2)

Redmega
Redmega

Reputation: 683

Your main issue is that you are attempting to access a property which does not exist.

Your object has many keys named Step_#, yet in this code: steps_array.Step_3[steps_array.Data] = {loc:{location : 4}}; you are attempting to access a .Data property, which doesn't exist. So you're trying to access the value of an undefined property.

Upvotes: 0

NikxDa
NikxDa

Reputation: 4187

First of all, your object is not an array - therefore you will be better off using the dot syntax. Considering that, what you want to do is probably:

steps_array.Step_3.Data = { loc: { location: 4 } };

Upvotes: 2

Related Questions