Leandro Cusack
Leandro Cusack

Reputation: 369

Javascript array index error

I am trying to access to a simple nested Array, doing this:

var currMenu = 1;
    while ( currMenu < menu.length ) {
        alert(currMenu);
        alert(menu[0][currMenu].text);
        currMenu++;
}

Despite alerts are throwing the correct values, I am getting this error on firebug: TypeError: menu[0][currMenu] is undefined.

What is happening?

Thanks!

Edit: Sorry, I was rushing, here you have the "menu" structure:

menu[0] = new Array();
menu[0][0] = new Menu(false, '', 15, 50, 20, '','' , 'navlink', 'navlink');
menu[0][1] = new Item('someText', '#', '', 100, 10, 1);

And the object Item:

function Item(text, href, frame, length, spacing, target) {
    this.text = text;
    if (href == '#') {
        this.href = '#';
    } else if (href.indexOf('http') == 0) {
        this.href = href;
    } else this.href = href;
    this.frame = frame;
    this.length = length;
    this.spacing = spacing;
    this.target = target;
    // Reference to the object's style properties (set later).
    this.ref = null;
    this.showLoadingBar = false;
}

Upvotes: 0

Views: 2806

Answers (2)

Pointy
Pointy

Reputation: 413916

You're looking at the length of the "menu" array, but you're accessing the array at the zero-th index of that array (which may or may not be an array; I can't tell from the code you've posted).

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382394

Assuming your menu is coherent with the [0][currMenu], you should access it like this :

while ( currMenu < menu[0].length ) {
    alert(currMenu);
    alert(menu[0][currMenu].text);

Upvotes: 3

Related Questions