我有一个类似的javascript类,
class Snake{
constructor(id, trail){
this.velocityX = 0;
this.velocityY = -1;
this.trail = trail;
this.id = id;
}
moveRight(){
console.log('move');
}
}
Run Code Online (Sandbox Code Playgroud)
和一个存储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));
Run Code Online (Sandbox Code Playgroud)
例如,我想从id为20的数组中删除该元素.
我怎样才能做到这一点?
那这个呢
this.snakeList = this.snakeList.filter(x => x.id != 20);
Run Code Online (Sandbox Code Playgroud)
let snakes = [{name: 'fuss', id: 10}, {name: 'huss', id: 20}, {name: 'hurr', id: 60}]
//Before removal
console.log("Before removal");
console.log(snakes);
snakes = snakes.filter(x => x.id != 20);
//After removal
console.log("After removal");
console.log(snakes);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
73 次 |
| 最近记录: |