如何更改从数组中删除的元素的 属性?
How to change property of element removed from array?
this.statues.shift();
正在隔离正确的元素并将其从数组中删除。但是,当发生这种情况时,person.stoned 需要为假。任何指针?谢谢。
代码:
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;
}
};
您需要保存移位的对象并设置它的 属性,像这样:
if(this.statues.length > 3) {
const unstoned = this.statues.shift();
unstoned.stoned = false;
}
由于对象是通过引用传递的,您可以使用 shift
的 return 值进行内联
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); }
this.statues.shift();
正在隔离正确的元素并将其从数组中删除。但是,当发生这种情况时,person.stoned 需要为假。任何指针?谢谢。
代码:
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;
}
};
您需要保存移位的对象并设置它的 属性,像这样:
if(this.statues.length > 3) {
const unstoned = this.statues.shift();
unstoned.stoned = false;
}
由于对象是通过引用传递的,您可以使用 shift
的 return 值进行内联 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); }