so this question might be a little basic on the syntax level but I am new to javascript and still working things out.
So I set up an array that looks like this:
chainResidue = [ {cname : selectedId.ChainId, rnum: Numbed(selectedId.residueNumber}]
I will be pushing that to a separate array called ballsAndSticksData = []
like so:
ballsAndSticksData.push(picked);
I want to check if that item is in the list then if so remove it.
Full code:
$('span').click(function() {
// Index the span element relative to the Div Class
var index = $(this).index();
var selectedId = chainResidue[index];
//Set up the picked structure **Note** Not every PDB file starts off at the index of 1
var picked = structure.select({
cname: selectedId.chainId,
rnum: Number(selectedId.residueNumber)
});
// Check if the span element is selected or not
if ($(this).hasClass('spanSelected')) {
$(this).removeClass('spanSelected');
$(this).addClass('spanUnselected');
//Function to remove an element in a list
var index = ballsAndSticksData.indexOf(picked);
alert (index);
if (index > -1) {
ballsAndSticksData.splice(index, 1);
}
console.log(ballsAndSticksData);
if ( ballsAndSticksData.length === 0 ) {
viewer.requestRedraw();
} else {
for (i = 0; i < ballsAndSticksData.length; i++) {
viewer.ballsAndSticks('ligand', ballsAndSticksData[i]);
}
}
}
else {
$(this).removeClass('spanUnselected');
$(this).addClass('spanSelected');
viewer.ballsAndSticks('ligand', picked);
ballsAndSticksData.push(picked);
console.log((ballsAndSticksData));
}
Things I have tried:
so the indexOf returns -1 everytime for the item in the list, so it must be something with the use of the {} which I thought was okay.