使用javascript循环javascript嵌套对象

时间:2014-09-06 13:08:39

标签: javascript loops object

这是我的对象在控制台中的样子:

Object
players: Array[5]
    0: Object
        user_c_x: 0
        user_c_y: 25.1
        username: "shuraa"
        __proto__: Object

    1: Object
        user_c_x: 0
        user_c_y: 25.1
        username: "maarten"
        __proto__: Object

    2: Object
        user_c_x: 0
        user_c_y: 25.1
        username: "maarten2"
        __proto__: Object

    3: Object
        etc.

我正试图使用​​以下方法遍历对象:

for (var key in user_data) {
    console.log(key);
}

返回"玩家"。我似乎无法访问下面的数组'。

有人可以帮助我访问user_c_x,user_c_y和用户名的值吗?

4 个答案:

答案 0 :(得分:2)

for (var key in user_data)将返回user_data中属性的名称,因此您可以使用它来获取实际的属性值。

你如何循环取决于你是否知道玩家肯定是该物业的名称,或者它是否可以有任何名称。

我假设你的user_data对象如下所示:

var user_data = {
    players: [
        {
                user_c_x: 0,
                user_c_y: 25.1,
                username: "shuraa"
        },
        {
                user_c_x: 0,
                user_c_y: 25.1,
                username: "maarten"
        },
        {
                user_c_x: 0,
                user_c_y: 25.1,
                username: "maarten2"
        }
    ]
};

如果你知道结构,你可以直接访问它:

for(var playerIndex in user_data.players)
{
    var player = user_data.players[playerIndex];
    console.log("username: ", player.username);
}

如果您不知道名称,可以像这样嵌套for循环:

for(var key in user_data)
{
    for(var playerIndex in user_data[key])
    {
        var player = user_data[key][playerIndex];
        console.log("username: ", player.username);
    }
}

答案 1 :(得分:0)

如果您共享对象但基于控制台数据players是一个数组会更容易。所以包含它的var应该是这样的:

var players =[ {user_c_x: 0, user_c_y: 25.1, username: "shuraa"}, {user_c_x: 0, user_c_y: 25.1, username: "maarten"}  ];

因此,循环投掷可以使用简单的for(i=0;i<players.length;i+=)

for(i=0;i<players.length;i++){ 
    console.log(players[i].username); 
}

答案 2 :(得分:0)

<强>的Javascript

var players  =[ {user_c_x: 0, user_c_y: 25.1, username: "shuraa"}, {user_c_x: 0, user_c_y: 25.1, username: "maarten"}  ]
for (var i=0; i<players.length; i++) {
    for(var innerObj in players[i])
    {
        $("body").append(innerObj +":"+players[i][innerObj] +"<br />");
    }
}

DEMO

答案 3 :(得分:0)

或者我可以尝试这个

    function findFox(map) {
    let uber = {}
    let fox = {}
    for (let i = 0; i < map.length; i++){
        for (let j = 0; j < map[i].length; j++){
            if (map[i][j] == ‘UBER’){
                uber.i = i
                uber.j = j
            } else if (map[i][j] == ‘FOX’){
                fox.i = i
                fox.j = j
            }
        }
    }

    let coor2 = []
    let coor = []

    for (let i = 0; i < map.length; i++){
        for (let j = 0; j < map[i].length; j++){
            if (map[i][j] == ' ' || map[i][j] == ‘FOX’){
                coor.push(i)
                coor.push(j)
                coor2.push(coor)
                coor = []
            }
        }
    }
    // console.log(coor2)

    let way = []
    let pos = {
        i: uber.i,
        j: uber.j
    }
    for (let i = 0; i < coor2.length; i++){
        let check = Math.abs((pos.i - coor2[i][0]) + (pos.j - coor2[i][1]))
        if (check == 1){
            if (pos.i == coor2[i][0] || pos.j == coor2[i][1]){
                // console.log(pos)
                // console.log(coor2[i])
                if (pos.i == coor2[i][0] && pos.j < coor2[i][1]){
                    way.push(‘kanan’)
                } else if (pos.i < coor2[i][0] && pos.j == coor2[i][1]){
                    way.push(‘bawah’)
                } else if (pos.i == coor2[i][0] && pos.j > coor2[i][1]){
                    way.push(‘kiri’)
                } else if (pos.i > coor2[i][0] && pos.j == coor2[i][1]){
                    way.push(‘atas’)
                }
                pos.i = coor2[i][0]
                pos.j = coor2[i][1]
                coor2[i][0] = 9
                coor2[i][1] = 9
                i = -1
            }
        }
        if (pos.i == fox.i && pos.j == fox.j){
            way = way.join(‘, ‘)
            console.log(way)
            console.log(‘’)
            break;
        }
    }