1

If I have an array, I can set the keys by doing the following:

var example:Array = new Array();

example[20] = "500,45";
example[324] = "432,23";

If I want to do something with Objects, how would I achieve this?

I tried the following:

var example:Object = [{x:500, y:45}, {x:432, y:23}]; // Works but keys are 0 and 1

var example:Object = [20: {x:500, y:45}, 324: {x:432, y:23}]; // Compile errors

var example:Object = [20]: {x:500, y:45}, [324]: {x:432, y:23}; // Compile errors

var example:Object = [20] {x:500, y:45}, [324] {x:432, y:23}; // Compile errors

Is there a good way to achieve this?

I understand I could do this:

var example:Object = {id20 : {x:500, y:45}, id324: {x:432, y:23} };

But it doesn't suit me.

1 Answer 1

2

The [] notation has the same meaning of doing a new Array() so when you are doing:

var example:Object = [{x:500, y:45}, {x:432, y:23}];

you are in fact creating an array with two elements who are object {x:500, y:45} and {x:432, y:23}.

If you want to create an object with key 20 and 324 use the {} notation who is the same a new Object()

So your example became =>

var example:Object = {20: {x:500, y:45}, 324: {x:432, y:23}};

You can do the same as your first example using an Object instead of an Array:

var example:Object = new Object();

example[20] = "500,45";
example[324] = "432,23";
Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely awesome. I wish that there was a shorthand notation for this though, but it's great feedback and it works so I'm not complaining! Thanks!

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.