简体   繁体   English

删除数组对象而不循环所有数组

[英]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. 我希望能够从一个数组中删除一个对象,而无需循环所有对象数组,以查看当前数组元素是否具有我要删除的项目的ID。

javascript: 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. 现在,用户可以在显示每本书的div中单击X按钮,以便删除该书。 So the X button contains: 因此,X按钮包含:

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. 现在显然在DelBook(divID)函数中,我可以遍历BooksArray的长度,并查看每个元素的divID是否等于参数和此时的拼接,但我想避免循环。

Is there any way to do it ? 有什么办法吗?

thanks in advance 提前致谢

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 希望能帮助到你

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 如果您确实希望删除O(1),则需要使用某种哈希,这将增加创建和更新数组的额外开销

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM