I want to be able to delete an object from an array without looping all the array of objects to see if the current array element has the ID of the item I want to delete.
javascript:
function CBooks() {
this.BooksArray = [];
this.AddBook = function(divID, sContents) {
this.BooksArray.push(new CBook());
pos = this.BooksArray.length - 1;
this.BooksArray[pos].ArrayID = pos;
this.BooksArray[pos].DivID = divID;
this.BooksArray[pos].Contents = sContents;
}
this.DelBook = function(divID) {
this.BooksArray.splice(...);
}
}
function CBook() {
this.ArrayID = 0;
this.DivID = "";
this.Contents = "";
}
I initialize the object like this:
var oBooks = new CBooks();
I add a new book like this :
oBooks.AddBook("divBook1", "blahblahblah");
//Creation of the div here
oBooks.AddBook("divBook2", "blehblehbleh");
//Creation of the div here
Now the user can click an X button in the div displaying each book, so that he can delete the book. So the X button contains:
onclick=oBooks.DelBook(this.id);
Now obviously in the DelBook(divID) function I could loop through the length of the BooksArray and see each element if it's divID is equal to the parameter and splice at that point, but I want to avoid the looping.
Is there any way to do it ?
thanks in advance