[英]How to change property of element removed from array?
this.statues.shift();
正在隔離正確的元素並將其從數組中刪除。 但是,當這種情況發生時, person.stoned 需要為 false。 任何指針? 謝謝你。
代碼:
class Medusa {
constructor(name) {
this.name = name;
this.statues = [];
}
stare(person) {
person.stoned = true;
this.statues.push(person);
if(this.statues.length > 3) {
this.statues.shift();
}
}
};
class Person {
constructor(name) {
this.name = name;
this.stoned = false;
}
};
您需要保存移位的 object 並設置其屬性,如下所示:
if(this.statues.length > 3) {
const unstoned = this.statues.shift();
unstoned.stoned = false;
}
由於對象是通過引用傳遞的,因此您可以使用 shift 的返回值進行內聯操作
if(this.statues.length > 3) {
(this.statues.shift()).stoned = false
}
if(this.statues.length > 3) { this.statues[0].stoned=false; this.statues.shift(); }
或者
if(this.statues.length > 3) { this.statues[0].stoned=false; this.statues.splice(0,1); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.