0

I have the following object containing arrays : Example data

I am iterating over it to get its key value like to use it further, Using for in to get the data inside of it.

for (let key in gameObj){
    console.log(key);
}

But its not even getting into the loop in order to get the arrays.
What am i missing ?

Gammer
  • 5,453
  • 20
  • 78
  • 121

3 Answers3

2

I would add this a comment but don't have the reputation yet. See Is it possible to get the non-enumerable inherited property names of an object?

function getAllProperties(obj){
    var allProps = []
      , curr = obj
    do{
        var props = Object.getOwnPropertyNames(curr)
        props.forEach(function(prop){
            if (allProps.indexOf(prop) === -1)
                allProps.push(prop)
        })
    }while(curr = Object.getPrototypeOf(curr))
    return allProps
}
R.Laney
  • 174
  • 8
1

It is working fine. Check the fiddle.

        
    var gameObj = {
    "key1" : ["1","1","1","1","1","1","1"],
    "key2": ["2","2","2","2","2","2","2","2","2","2",]
    }
    
    for(var i in gameObj){
    alert(i);
    }
jatinder bhola
  • 385
  • 1
  • 7
  • 23
1

If you want to access the object in a for loop key will be the key, then you just call it like you would an array:

const gameObj = {
   '99lEEbmV7s': ['37966', '37966', '37965', '37966', '0'],
   'TggZdsbcje': ['37966', '37966', '37965', '37966', '0']
};

for (let key in gameObj){
    console.log(gameObj[key]);
}
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338