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.
2 Answers 2
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.)
Comments
The first object is not an array, it's an object. Do it like this:
test.theme = {
default: ['blue','pink','orange']
}
1 Comment
.push()
or .slice()
or .length
on your "test.theme" object, but that may be what you want.
test.theme.length==0
. In second example is not valid syntax. Do you want an object?