views:

28

answers:

1

For example if i'm keeping array of references via id like that:

if(typeof channel_boards[misc.channel_id] == 'undefined') {

    channel_boards[misc.channel_id] = $('<div class="channel" channel_id="'+misc.channel_id+'"></div>').appendTo('#board');
}

And then i'm looping through array to find required reference. I'm looping through undefined properties as well. Is it possible to loop only through defined properties?

for(i=0;i<channel_boards.length;i++)
{
    if(channel_boards[i] != undefined)
    {
        if(channel_boards[i].attr('channel_id') != visible) {channel_boards[i].addClass('hidden_board');}
        else {channel_boards[i].removeClass('hidden_board');}       
    }
}

Maybe i should change the way i'm storing references? Via object for example, but how i'll be able to find proper reference via id number.

+1  A: 

It does sound like you would be better of using an object to store the references

var channel_boards = {};
var channel_id = 1;
// add property
channel_boards["channel_" + channel_id] = ......

// enumerate properties
for (var key in channel_boards) {
    if (channel_boards.hasOwnProperty(key) {
        channel_boards[key].attr(......
    }
}

// delete property
delete channel_boards["channel_" + channel_id];
Sean Kinsey
Damn i'm dumb :)Thanks, just need to add prefix. :)
Beck
The prefix isn't needed, anything that can be converted to a string will do (including numbers). So you could get away with `..[channel_id]`
Sean Kinsey