Skip to content Skip to sidebar Skip to footer

How To Iterate Over This Object

I have this object: var navArray = { '#item1' : 0, '#item2' : 1, '#item3' : 2, '#item4' : 3, '#item5' : 4, '#item6' : 5 } The ident var in the code below is a number a

Solution 1:

This should work:

var ident = 1,
    target = null;
for (var key in navArray) {
    if (navArray.hasOwnProperty(key)) { 
        if (navArray[key] === ident) {
            target = key;
            break;
        }
    }
}

alert (target); // "#item2"

Post a Comment for "How To Iterate Over This Object"