Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating thru Object properties produces different results in different browsers [duplicate]

I am creating a very basic object in JavaScript and looping thru its properties, displaying property name:

var name = {
                'A': 'DataA',
                'B': 'DataB',
                'C': 'DataC',
                'D': 'DataD',
                'E': 'DataE'
            }

for (var propName in name) {
    document.getElementById('result').innerHTML += propName + ' '
}

In IE and FireFox it produces expected result:

A B C D E 

But in Chrome same code produces

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Any idea why? Does keyword name hold some significance in Chrome?

like image 629
Yuriy Galanter Avatar asked Jun 15 '13 00:06

Yuriy Galanter


People also ask

Which line of code is used to iterate through all the properties of an object?

The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.


1 Answers

Chrome doesn't seem to like it when you use it as a global variable (there's also a name property on the window object). Just run it inside a function.

like image 133
zdyn Avatar answered Oct 04 '22 15:10

zdyn