【发布时间】:2019-09-13 05:34:02
【问题描述】:
我正在学习 Javascript,并且正在制作一个简单的用户验证对象。我需要一个函数来 console.log 仅将“isVerified”设置为 true 的用户。
我尝试了很多方法,包括循环和 if 语句。下面的函数名是“showVerified”。
var person = {
info: [],
displayPerson: function() {
console.log('People', this.info);
},
addPerson: function(age, firstName, lastName) {
this.info.push({
age: age,
firstName: firstName,
lastName: lastName,
isVerified: false
});
this.displayPerson();
},
deletePerson: function(position) {
this.info.splice(position, 1);
this.displayPerson();
},
verifyPerson: function(position) {
this.info[position].isVerified = true;
this.info[position].firstName = this.info[position].firstName.toUpperCase();
this.displayPerson();
},
showVerified: function() {
for (var key in this.info) {
if (this.info.isVerified = true) {
console.log(this.info.isVerified[key]);
}
}
}
}
在我的 person 对象上运行 showVerified 时,我希望它只打印任何经过验证的人的年龄、名字和姓氏。
【问题讨论】:
-
this.info.isVerified = true应该是this.info.isVerified === true -
单个
=是一个赋值,===是一个比较。你也可以使用双刘海if(!!this.info.isVerified) -
不清楚您在问什么,因为我们看不到整个代码。
this.info是什么?position是什么? -
整个代码都在那里。这些是我的 person 对象中的函数。
标签: javascript