1

I have the following code:

    data_array =  {
                      full_name: 'fullname',
                      items: [],
                      address_full: 'address'
                  };

        first = {
            'data-opPrice'  : '1a',
            'data-stdPrice' : '1b',
            'state'         : '1c'
        };
        second = {
            'data-opPrice'  : '2a',
            'data-stdPrice' : '2b',
            'state'         : '2c'
        };          
        data_array.items.push(first);
        data_array.items.push(second);

    alert(data_array['items'][1].data-opPrice);

I would expect to get the alert "2a". But nothing happens. Why?

2

5 Answers 5

4

It's because of the - in data-opPrice. - is subtraction.

alert(data_array['items'][1]['data-opPrice']);

http://jsfiddle.net/t9c7L/1/

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

Comments

1

You probably want:

data_array.items[1]['data-opPrice'];

Comments

0

Try

alert(data_array['items'][1]['data-opPrice']);

because otherwise it's an operation ( data minus opPrice)

Comments

0

use this:

data_array.items[1]['data-opPrice']

Comments

0

use the naming conventions and don't use dash (minus) on properties name.

So follow these rules: http://javascript.crockford.com/code.html

and then write a code like this:

data_array =  {
                  full_name: 'fullname',
                  items: [],
                  address_full: 'address'
              };

    first = {
        'dataOpPrice'  : '1a',
        'dataStdPrice' : '1b',
        'state'         : '1c'
    };
    second = {
        'dataOpPrice'  : '2a',
        'dataStdPrice' : '2b',
        'state'         : '2c'
    };          
    data_array.items.push(first);
    data_array.items.push(second);

alert(data_array.items[1].dataOpPrice);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.