迭代匿名对象

时间:2017-11-22 20:40:00

标签: javascript arrays loops object

我有以下包含数组的对象: Example data

我正在迭代它以获得其关键值,以便进一步使用它,使用for in获取其中的数据。

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

但它甚至没有进入循环以获得阵列 我错过了什么?

3 个答案:

答案 0 :(得分:1)

我会添加一条评论但尚未获得声誉。 见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
}

答案 1 :(得分:0)

工作正常。检查小提琴。

        
    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);
    }

答案 2 :(得分:0)

如果你想在(...)循环中访问该对象for将成为关键,那么你就像调用数组一样调用它:

key