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
-
4Why do you want to avoid the looping, is there an acute performance issue? If no: just do the looping. If yes: use some kind of hash bucket system? What did you have in mind?Halcyon– Halcyon2013年05月21日 21:07:20 +00:00Commented May 21, 2013 at 21:07
-
I want to avoid the looping because I thought that there may be some faster method to just access that element directly with the help of javascript associative arrays.MirrorMirror– MirrorMirror2013年05月21日 21:09:30 +00:00Commented May 21, 2013 at 21:09
-
2if you dont want looping, u need use a dictionary or a similar hash data estructure.levi– levi2013年05月21日 21:10:19 +00:00Commented May 21, 2013 at 21:10
-
You could keep an index I guess. Try it?Halcyon– Halcyon2013年05月21日 21:10:33 +00:00Commented May 21, 2013 at 21:10
-
This is a very common question around here. You either have to do the looping yourself, or you can use a library that does looping. Or... as @FritsvanCampen has mentioned, you could use some sort of hashing system or you could store an index in the book objects.jahroy– jahroy2013年05月21日 21:10:58 +00:00Commented May 21, 2013 at 21:10
3 Answers 3
Something like this would work, but only if you are willing to abandon the array used for a hash.
Your code edited
function CBooks() {
this.BooksHash = {};
this.AddBook = function(divID, sContents) {
var book = new CBook();
//book.ArrayID = pos; //you don't actually need this anymore using a hash
book.DivID = divID;
book.Contents = sContents;
this.BooksHash[book.DivID] = book;
}
this.DelBook = function(divID) {
delete this.BooksHash[divID];
}
}
function CBook() {
//this.ArrayID = 0; // same here
this.DivID = "";
this.Contents = "";
}
Hope it helps
2 Comments
arr.filter(function(item){
Return item.id != idtoremove
});
That will loop under the covers, but uses fast native code and is easier to read. If you really want O(1) delete you'll need to use some sort of hash and will add extra overhead on creating and updating the array
1 Comment
Array.filter isn't supported on IE8, if that matters to the OP.I solved it like this:
function CBooks() {
this.BooksArray = [];
this.Hashes = {};
this.AddBook = function(divID, sContents) {
this.BooksArray.push(new CBook());
pos = this.BooksArray.length - 1;
this.BooksArray[pos].ArrayID = pos;
this.Hashes[divID] = pos;
this.BooksArray[pos].DivID = divID;
this.BooksArray[pos].Contents = sContents;
}
this.DelBook = function(divID) {
this.BooksArray.splice(this.Hashes[divID], 1);
}
}
function CBook() {
this.ArrayID = 0;
this.DivID = "";
this.Contents = "";
}
7 Comments
divIDs will be in there as keys. That's easily enough fixed by creating an array of the values when you have to save (which shouldn't be near as often as you'll be looking up stuff).