【发布时间】:2019-05-11 06:30:21
【问题描述】:
我有一个类似的 javascript 类,
class Snake{
constructor(id, trail){
this.velocityX = 0;
this.velocityY = -1;
this.trail = trail;
this.id = id;
}
moveRight(){
console.log('move');
}
}
还有一个存储 Snake 对象的数组。
this.snakeList = new Array();
this.snakeList.push(new Snake(10, newSnakeTrail));
this.snakeList.push(new Snake(20, newSnakeTrail));
this.snakeList.push(new Snake(30, newSnakeTrail));
this.snakeList.push(new Snake(22, newSnakeTrail));
this.snakeList.push(new Snake(40, newSnakeTrail));
例如,我想从数组中删除 id 为 20 的元素。
我该怎么做?
【问题讨论】:
-
使用过滤器将其删除?
-
您在问 2 个问题。首先 - 是如何通过
key/value在数组中查找对象。第二 - 如何从数组中删除一个项目。
标签: javascript arrays