Ich habe eine Javascript-Klasse wie:
class Snake{
constructor(id, trail){
this.velocityX = 0;
this.velocityY = -1;
this.trail = trail;
this.id = id;
}
moveRight(){
console.log('move');
}
}
und ein Array, das Schlangenobjekte speichert.
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));
Zum Beispiel möchte ich das Element aus dem Array entfernen, dessen ID 20 ist.
Wie kann ich das machen?
Was ist damit?
this.snakeList = this.snakeList.filter(x => x.id != 20);
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);
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen