function initializeItems($items, current, count) {
$items.removeClass('z-item-current');
$items.each(function(index) {
var $self = $(this);
$self.data('z-item-index', index);
if(index === current) {
$self.addClass('z-item-current z-item-show');
var pre = current - 1,
next = current + 1;
pre = pre < 0 ? count - 1 : pre;
next = next > count - 1 ? 0 : next;
$($items[pre]).addClass('z-item-previous z-item-show');
$($items[next]).addClass('z-item-next z-item-show');
}
});
// reselect or filter $items
// if has z-item-show class, show it; otherwise, hide it.
};
Looking at the code above, the objective is to reselect/filter a jQuery object. Specifically, the goal is to divide $items
into two categories: $items with z-item-show class
and $items without z-item-show class
.
Currently, one approach is using $items.each
, while another is using $items.parent().find
.
Are there any more elegant approaches to achieve this?