Is it really so? This little snippet shows that Object.keys doesn't really enumerate keys but rather ... values.
var p = { foo : 0, bar : 1 }; for ( var e in p ) { console.log( e ); } for ( var e in Object.keys( p ) ) { console.log( e ); }The snippet prints
foo bar 0 1while it definitely should print
foo bar foo barWhat's wrong here?
1 comment:
I think you need to use the hasOwnProperty function. That is, if(p.hasOwnProperty(e))...
Also, using 0 and 1 was a bit of a red herring because they are the indexes of the items in the resulting array, as well as the values in the object :)
Post a Comment