例如,如果我通过id保存引用数组:
if(typeof channel_boards[misc.channel_id] == 'undefined') {
channel_boards[misc.channel_id] = $('<div class="channel" channel_id="'+misc.channel_id+'"></div>').appendTo('#board');
}
然后我循环遍历数组以找到所需的引用。我也循环遍历未定义的属性。是否可以仅通过已定义的属性进行循环?
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');}
}
}
也许我应该改变我存储引用的方式?例如,通过对象,但我将如何通过ID号找到适当的引用。
答案 0 :(得分:1)
听起来你最好使用一个对象来存储引用
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];