简体   繁体   English

javascript 从数组中删除项目

[英]javascript removing items from array

I have "scene" with graphics "objects"...我有带有图形“对象”的“场景”......

Scene.prototype.objects=new Array();

Scene.prototype.add=function(obj){
  var last=this.objects.length;
  this.objects[last]=obj}

Scene.prototype.remove=function(obj){
  this.objects.splice(obj.id,1)}

Scene.prototype.advance=function(){
  for (var id in this.objects){
    var obj=this.objects[id];
    obj.id=id;
    obj.advance();
  }
}
Scene.prototype.paint=function(context){...}

each time creating and deleting many objects.每次创建和删除许多对象。 Array.prototype.splice re-index array right? Array.prototype.splice 重新索引数组对吗? Does anyone know a better technique (adding and removing on javascript Array)?有谁知道更好的技术(在 javascript 阵列上添加和删除)?

In my opinion, is another possibility to do that something like在我看来,是否有另一种可能性来做类似的事情

Scene.prototype.remove=function(obj){
  delete this.objects[obj.id]; // don,t care about this.objects.length
  delete obj; // not necessary...
}

I have not tried it yet...我还没试过。。。

I need a good book about JavaScript:)我需要一本关于 JavaScript 的好书:)

Your delete method wouldn't work, because objects is an Array, and obj.id is the id of the object reference stored in an element in that Array.您的删除方法不起作用,因为objects是一个数组,而 obj.id 是存储在该数组中的元素中的 object 引用的 id。 splice would be the method to use, but you'll have to know the index of the element in the Array. splice将是使用的方法,但您必须知道数组中元素的索引。 Maybe you should 'remeber' it this way:也许你应该这样“记住”它:

Scene.prototype.add=function(obj){
  var last=this.objects.length;
  obj.objectsIndex = last;
  this.objects[last]=obj
}

After which you can:之后,您可以:

Scene.prototype.remove=function(obj){
  this.objects.splice(obj.objectsIndex,1)};
  //reindex the objects within the objects Array
  for (var i=0; i<this.objects.length;i++){
     this.objects[i].objectsIndex = i;
  }
}

Note : Adding the objects Array to the prototype of your Scene constructor means it will be the same for all instances (static), is that what you want?注意:将objects数组添加到Scene构造函数的原型意味着它对于所有实例(静态)都是相同的,这是您想要的吗?

I'd rather avoid using new Array() instead use [] (page 114 of book I mentioned in comment - 'JavaScript: The Good Parts' by Douglas Crockford;)).我宁愿避免使用new Array()而是使用[] (我在评论中提到的书的第 114 页 - Douglas Crockford 的“JavaScript:The Good Parts”;))。

What's more, I don't think adding objects array to Scene prototype will work as you expect.更重要的是,我认为将objects数组添加到场景原型中不会像您期望的那样工作。 You probably want to achieve some Object-Oriented structure, which requires some acrobatic skills in JavaScript, as it uses prototypal inheritance as it is class-free.您可能想要实现一些面向对象的结构,这需要 JavaScript 中的一些杂技技能,因为它使用原型 inheritance ,因为它是无类的。

Try something like this:尝试这样的事情:

var Scene = function(initial_objects) {
    this.objects = initial_objects || [];
};

Scene.prototype.add = function(obj) {
    this.objects.push(obj);
};

Scene.prototype.remove_by_index = function(index) {
    this.objects.splice(index, 1);
};

Scene.prototype.remove = function(obj) {
    var index = this.objects.indexOf(obj);
    if (index > -1) {
        this.objects.splice(index, 1);
    }
};

Read also this: http://javascript.crockford.com/inheritance.html另请阅读: http://javascript.crockford.com/inheritance.html

It seems you don't actually need an array for your objects.看来您实际上并不需要对象数组。 You can use another object has hash table:您可以使用另一个 object 具有 hash 表:

(function() {

    var i = 0;

    Scene.prototype.objects = {};

    Scene.prototype.add = function(obj) {
      var id = i++;
      this.objects[id] = obj;
      obj.id = id;
    };

    Scene.prototype.remove = function(obj){
      if(obj.id in this.objects) {
          delete this.objects[obj.id];
      }
    };

    Scene.prototype.advance=function(){
      for (var id in this.objects){
        var obj=this.objects[id];
        obj.id=id;
        obj.advance();
      }
    };

    Scene.prototype.paint=function(context){...}

}());

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

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