迭代node.js中Objects中的值

时间:2014-02-07 11:48:36

标签: javascript node.js

我只想迭代对象中的值。场景就像这样!

roomsObject[socket.nickname]={"roomN":users[socket.nickname].roomName
                             ,"Clients":['A','B','C']};

其中roomsObject是一个对象,我想遍历客户端。

 function updateRoomList(groupO,callback)
{
    roomParticularGroupList=[];
    for(var key in roomsObject)
    //this iterates through socket.nickname but i want to iterate through  roomsObject[socket.nickname].clients so that i get list of clients and send it back through callback  
    roomParticularGroupList.push(roomsObject[socket.nickname].clients);
    }
    console.log(roomParticularGroupList);
    callback(roomParticularGroupList);

}

1 个答案:

答案 0 :(得分:0)

for (var key in roomsObject)
    roomParticularGroupList.push(roomsObject[socket.nickname].clients);
}

此处没有socketnickname属性,您在循环中枚举 key s ,您必须使用它。此外,您已小写clients。它应该是

for (var key in roomsObject)
    roomParticularGroupList.push(roomsObject[key].Clients);
}
顺便说一下,这里几乎没有理由使用回调。只需return roomParticularGroupList