2

I am trying to understand something very basic. If I have an object like this:

var topics = {}

And I do this:

topics[name] = ["chapter 1", "chapter 2", "chapter 3"];

When I log this object, I don't see the name attribute. What have I done exactly? Have I created a key called name with the value of an array?

Of course I know I can do that by just doing

topics.name =  ["chapter 1", "chapter 2", "chapter 3"];

But then what is this doing?

topics[name] = ["chapter 1", "chapter 2", "chapter 3"];

Could someone please clarify?

1
  • This doesn't answer you question but I believe you really want to ask the difference between topics[name] = ["chapter 1", "chapter 2", "chapter 3"]; and topics["name"] = ["chapter 1", "chapter 2", "chapter 3"];? Commented Oct 21, 2011 at 13:26

6 Answers 6

3

When you use the [] notation it expects an expression in between, that translates to a string.

Using name not enclosed in quotes 'name' it assumes you are using a variable called name.

Which in your case is undefined.

The correct usage would be

topics["name"] = ["chapter 1", "chapter 2", "chapter 3"];

If you want to use a variable you can do things like

var prop = 'name';
var topics = {};

topics[prop] = ["chapter 1", "chapter 2", "chapter 3"];

this will create the name property on the object.. useful for dynamic/automatic creation/filling of objects..

Sign up to request clarification or add additional context in comments.

2 Comments

Does that mean I can assign a function with expressions that evaluate to a string (like a topicName) or a number (like a topicID)?
@Amit, yes you can do topics[func()] = '...'; if func() returns something that can be converted to string.. (if that is what you are asking..)
3

Your are creating a property on the object with a name based on the value of the name variable. If you want to create a property called name in that way you need to do:

topics["name"] = ["chapter 1", "chapter 2", "chapter 3"];

Comments

3

That should generate an error, unless you have variable name defined as a string or a number.

These three are equivalent:

var name = "key";
topics[name] = "value";

topics["key"] = "value";

topics.key = "value";

Comments

2

There are generally three ways to distinguish:

topics.name
topics["name"]
topics[name]

The first two are equivalent. So .xxx represents the literal key xxx, as does ["xxx"].

The last one will use whatever the name variable contains. E.g.:

var name = "test";
topics[name] = 123; // topics["test"] now exists

Comments

2

The resulting structure can be seen in the following screen capture. As stated by mck89, you probably want to use the "name" syntax

enter image description here

3 Comments

thanks for the comparative screenshots. That made things a lot clear
Don't use JPEG for screenshots. it makes them all fuzzy.
@DanD. sorry about that... It usually outputs as a png... I wonder why it didn't that time.
1

If your code did not result in a ReferenceError, you may have already had a variable in global scope named name, and created a new property of name's contents.

Comments

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.