10

This is what I'm trying to build via JavaScript in dot or [ ] notation:

var shoppingCart = { 
        'item1' : {
            'description' : 'This is item #1',
            'price' : 10,
            'quantity' : 1,
            'shipping' : 0,
            'total' : 10
        }
    };

Now if 'item1' is the variable name itemName.

This works:
var shoppingCart = {};
shoppingCart[itemName] = itemName;
alert(shoppingCart.item1);

Which returns item1

But this doesn't work:
1 var shoppingCart = {};
2 shoppingCart[itemName]['description'] = 'This is Item #1';

JS just dies at line 2, why? and how do I assign the description value to 'description'?

I would do it like this:

var shoppingCart = { 
        itemName : {
            'description' : description,
            'price' : price,
            'quantity' : quantity,
            'shipping' : shipping,
            'total' : total
        }
    };

...but it makes the key literally itemName instead of item1.

4 Answers 4

13

shoppingCart[itemName] doesn't exist.
You need to create it first:

var shoppingCart = {};
shoppingCart[itemName] = { };
shoppingCart[itemName].description = 'This is Item #1';

Or, better yet:

var shoppingCart = {};
shoppingCart[itemName] = { description: 'This is Item #1' };
Sign up to request clarification or add additional context in comments.

2 Comments

Also shoppingCart[itemName]['description'] is equivalent to shoppingCart[itemName].description. You only need to access properties with [] when accessing them dynamically obj[propNameString] or when it contains illegal characters for dot notaton obj['some-prop-name']
Yes thnx, that's what I read everywhere, but the key is that shoppingCart[itemName] = { }; I'm understanding objects more and more each day!
2

In javascript objects do not auto-create on member access; you have to create the object first:

var shoppingCart = {};
shoppingCart["item1"] = {}; // This creates the object
shoppingCart["item1"]["description"] = "This is item #1"; // add a member

Note you can of course also create the whole object at once with

shoppingCart[itemname] = { "description": description,
                           "price": price,
                           "quantity": quantity,
                           "shipping": shipping,
                           "total": total };

Comments

1

It doesn't work because, in your second example, you're trying to index a blank object with a key that hasn't been set. The following will work...

var shoppingCart = {};
shoppingCart[itemName] = {};
shoppingCart[itemName]['description'] = '...';

Because now shoppingCart[itemName] is actually an object.

Comments

0

Just FYI, alternative solution:

var itemName = {};
itemName.description = 'This is Item #1';
var shoppingCart = {};
shoppingCart.itemName = itemName;

or just

var shoppingCart = {};
shoppingCart.itemName = { description: 'This is Item #1' };

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.