0

I have an empty array and can an add to it like so:

test.theme = [
];
test.theme.default = ['blue','pink','orange'];

How can I hard code default in to the theme array instead of first declaring the array and then adding to it?

I've been trying

test.theme = [
 default: ['blue','pink','orange']
]

Can't seem to get the right syntax.

asked Jan 12, 2014 at 21:11
1
  • First example you're attaching a property to the array object, not as an index of the array, so your test.theme.length==0. In second example is not valid syntax. Do you want an object? Commented Jan 12, 2014 at 21:13

2 Answers 2

3

The array literal syntax makes no provisions for attaching properties (other than the implicitly-numbered properties, of course). You have to add the "default" property in a different statement, and you'll have to do it like this:

test.theme["default"] = ['blue', 'pink', 'orange'];

if you don't want some IE versions to get upset. (The word "default" is a JavaScript keyword.)

answered Jan 12, 2014 at 21:13

Comments

2

The first object is not an array, it's an object. Do it like this:

test.theme = {
 default: ['blue','pink','orange']
}
answered Jan 12, 2014 at 21:13

1 Comment

@MikeRifgin Good - note that you won't be able to use .push() or .slice() or .length on your "test.theme" object, but that may be what you want.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.