javascript - delete an object of an array without looping all the array -


i want able delete object array without looping array of objects see if current array element has id of item want 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 object this:

var obooks = new cbooks(); 

i add new book :

obooks.addbook("divbook1", "blahblahblah"); //creation of div here obooks.addbook("divbook2", "blehblehbleh"); //creation of div here 

now user can click x button in div displaying each book, can delete book. x button contains:

onclick=obooks.delbook(this.id); 

now in delbook(divid) function loop through length of booksarray , see each element if it's divid equal parameter , splice @ point, want avoid looping.

is there way ?

thanks in advance

something work, if willing abandon array used hash.

your code edited

function cbooks() {   this.bookshash = {};    this.addbook = function(divid, scontents) {     var book = new cbook();     //book.arrayid = pos; //you don't need anymore using 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 helps


Comments

Popular posts from this blog

php - Dynamic url re-writing using htaccess -

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -