Length of empty array in javascript -
so i'm trying 1 of basic things, , wondering what's wrong approach.
i have object containing rooms
{ rooms: { ijxeczyu: { id: 'ijxeczyu', teachername: 'testteacher', teacherid: '/#ikosyfr1ntdcpkbaaaaa', clients: [] } }, getrooms: [function], addroom: [function] } now want render info table in jade. work, want extract important stuff , push array
function makearray(obj) { var roomarray = []; (var room in obj) { roomarray.push({ roomid : room.id, teacher : room.teachername, clients : room.clients.length || 0 }) } return roomarray; } pretty easy. but can't empty clients array work. typeerror: cannot read property 'length' of undefined isn't || 0part should catch?
why doesn't work? how can read length of array or 0 if there no entries?
room.clients undefined why error. reason it's undefined because room string.
a for..in loop iterates on keys of object. means room === "ijxeczyu", assuming you're passing in rooms object. access object, you'll have access object at key.
for (var name in obj) { var room = obj[name]; ... }
Comments
Post a Comment