MirrorMirror
MirrorMirror

Reputation: 188

delete an object of an array without looping all the array

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

Upvotes: 1

Views: 257

Answers (3)

MirrorMirror
MirrorMirror

Reputation: 188

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 = "";
}

Upvotes: 0

intuitivepixel
intuitivepixel

Reputation: 23322

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

Upvotes: 5

Ben McCormick
Ben McCormick

Reputation: 25728

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

Upvotes: 2

Related Questions