so i'm trying to do one of the most basic things, and i am wondering what's wrong with my approach.
i have an object containing rooms
{ rooms:
{ ijxeczyu:
{ Id: 'ijxeczyu',
teacherName: 'testteacher',
teacherId: '/#Ikosyfr1NtDcpKbaAAAA',
clients: [] } },
getRooms: [Function],
addRoom: [Function] }
now i want to render their info to a table in jade. for that to work, i want to extract the important stuff and push it to an array
function makeArray(obj) {
var roomArray = [];
for (var room in obj) {
roomArray.push({
roomId : room.Id,
teacher : room.teacherName,
clients : room.clients.length || 0
})
}
return roomArray;
}
pretty easy.
but i can't get that empty clients array to work.
TypeError: Cannot read property 'length' of undefined
isn't that exactly what the || 0part should catch?
why doesn't this work? how can i read the length of an array or 0 if there are no entries?
|| 0is too late. Instead, you can do:(room.clients || []).length