Here is a data structure to do what you want.
var DS = {
Items: {},
Groups: {},
setItem: functon(index, item, group){
this.Items[index] = item;
if ( typeof this.Groups[group] === 'undefined' )
this.Groups[group] = [index];
else
this.Groups[group].push(index);
},
isSelected: function(item) {
return item >= 0;
},
isSelectedGroup: function(group) {
var Group = this.Groups[group];
for ( var i in Group ) {
var Item = this.Items[i];
if ( this.isSelected(Item) ) {
return true;
}
}
},
getSelectedGroups: function(){
var selected = [];
for ( var group in this.Groups ) {
var Group = this.Groups[group];
if ( this.isSelectedGroup(Group) ) {
selected.push(group);
}
}
return selected;
}
}
To use with your items: 0,2,0,5,6,0,9. Do the following:
DS.setItem(0,0,1);
DS.setItem(1,2,1);
DS.setItem(2,0,1);
DS.setItem(3,5,2);
DS.setItem(4,6,2);
DS.setItem(5,0,2);
DS.setItem(6,9,3);
To test:
DS.isSelectedGroup(3);
// or to get all selected groups
DS.getSelectedGroups();
Should work, if not you should be able to figure it out :)