1

I am trying to use the string in a variable to create an item in an object.

Example non functioning code:

    private var myName:String = 'group1';

    private var _ins:Object = {
        myName : { 
            data1: arr[0],
            data2: arr[1]               
        }
    }

I can not understand what syntax to use to make myName be 'group1' text. Currently I am using:

    private var _ins:Object = {
        'group1' : { 
            data1: arr[0],
            data2: arr[1]               
        }
    }

1 Answer 1

3

Syntax of generic objects in AS3 allow to omit quotes for the keys, but actually your code is:

private var myName:String = 'group1';

private var _ins:Object = {
    "myName" : { 
        "data1": arr[0],
        "data2": arr[1]               
    }
}

You should use myName as a variable, which contains a key.

private var myName:String = 'group1';

private var _ins:Object = {};

_ins[myName] = { 
    data1: arr[0],
    data2: arr[1]               
};
Sign up to request clarification or add additional context in comments.

1 Comment

Great, I now understand.

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.