1

I have this array (I know, technically there is no such thing as a multidimensional array in javascript) or 'object'. I want to get the value -888 like so:

    var thisPage = 1;

    var pagePos = {
        0: {
            left: '0',
            url: 'home',
        },
        1: {
            left: '-888',
            url: 'what_we_offer',
        },
        2: {
            left: '-1776',
            url: 'clients',
        },
        3: {
            left: '-2664',
            url: 'contact_us',
        }
    };

alert(pagePos[thisPage].left);

It works fine in Firefox, but not IE. Why?

Thanks.

4
  • It does work in IE. Working demo. Is this your actual code? Something else is going wrong, something we're not seeing. Is there perhaps a trailing , after declaring the 3 property? Because that doesn't work very well in IE. Commented Nov 22, 2011 at 20:55
  • @DavidHedlund He didn't say what version OF IE... Commented Nov 22, 2011 at 20:56
  • Thanks for your reply, actually duri has it below- it's the trailing commas after the url attributes that's throwing it. Cheers. Commented Nov 22, 2011 at 21:01
  • 1
    @nex: fair point. Heh, with hindsight "something we're not seeing ... perhaps a trailing comma" should've been something I wasn't seeing o_O Commented Nov 23, 2011 at 7:26

4 Answers 4

2

This is because of trailing commas after values of url attributes. You can't have comma after the last value in JSON. The correct syntax is

var pagePos = {
    0: {
        left: '0',
        url: 'home'
    },
    1: {
        left: '-888',
        url: 'what_we_offer'
    },
    2: {
        left: '-1776',
        url: 'clients'
    },
    3: {
        left: '-2664',
        url: 'contact_us'
    }
};
Sign up to request clarification or add additional context in comments.

Comments

2

What if you turn it into an array of objects (since that's the way you're accessing it):

var thisPage = 1;

var pagePos = [
    {
        left: '0',
        url: 'home'
    },
    {
        left: '-888',
        url: 'what_we_offer'
    },
    {
        left: '-1776',
        url: 'clients'
    },
    {
        left: '-2664',
        url: 'contact_us'
    }
];

alert(pagePos[thisPage].left);

Also... remove the extra commas after the url properties. Some browsers are OK with them, some not.

1 Comment

Yes- it's the extra commas after the url properties! Thanks.
0

I can see a couple ways IE might have issues with this (it's IE, cmon'). Try changing the nature of your Array to a simple Array of objects. They'll be naturally numbered this way, by the order they're initialized.

var pagePos = [
    {
        left: '0',
        url: 'home',
    },
    {
        left: '-888',
        url: 'what_we_offer',
    },
    {
        left: '-1776',
        url: 'clients',
    },
    {
        left: '-2664',
        url: 'contact_us',
    }
];

Comments

0

That's kind of a quirky way to put together an array...I'm surprised it works in any browser! Try definining pagePos like this intead:

var pagePos = [ // note: bracket!
        {
            left: '0',
            url: 'home',
        },
        {
            left: '-888',
            url: 'what_we_offer',
        },
        {
            left: '-1776',
            url: 'clients',
        },
        {
            left: '-2664',
            url: 'contact_us',
        }
    ];

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.