The question is very broad, and more so when they never give the definition of the object, however I will answer some possible reasons why the code does not work correctly as expected.
Possible causes
The object definition defines 9
and 10
as non-enumerable
Browser with poor implementation of Console
9
and 10
return undefined
Quoting the code
console.log(object); // si muestra valores
console.log(Object.keys(object).length); //muestra 0
console.log(object['9']); //muestra undefined
Exemplifying
Object.keys
returns all properties enumerables
so if 9
and 10
are not enumerable, and are the only properties defined in object
then Object.keys(object)
will return an array of 0
elements, for more information see here
Suppose that the definition of object
is the following
var object = Object.defineProperties({},
{
'9': {enumerable: false, get: function(){ return 'soy 9' }},
'10':{enumerable: false, get: function(){ return 'soy 10'}}
})
As you can see 9
and 10
are non-enumerable properties, (no need to define it, because by default all property is created as non-enumerable and not configurable unless otherwise specified, so include enumerable: false
is redundant and only for didactic purposes)
Object.keys(object).length // 0
The previous line returns an empty array since object
has only properties no enumerables
, so Object.keys(object).length
is 0
for (var key in object) {
console.log(object[key]) // no hace nada
}
The previous for..in
does nothing, since for..in
only iterates on enumerable properties, see here
object["9"] // 'soy 9'
object["10"] // 'soy 10'
However, if you can access these properties, so it can not be due to a wrong browser, unless it is clear that 9
and 10
return undefined
when doing get