我想从数组中获取对象的值
I want to get a value of an object from an array
我有一项服务可以提供这样的数据。我正在尝试构建评论功能。
students:
{ id: string;
isEdit: boolean;
userName: any;
userBio: any;
profileImage: any; }[];
现在我正在尝试获取 userName 的值,类似于 Object.values 但只有 userName。
addComment(){
let currentUser = firebase.auth().currentUser;
this.comments.push(this.comment);
let data = {
comments: firebase.firestore.FieldValue.arrayUnion
({comment: this.comment, username: Object.values(this.students) })
}
this.firebaseService.updatePost(this.item.id,data)
}
上面的代码导致了这个
、
我怎样才能上传用户名的值?
所以我可以{评论:"sdd",用户名:"snfs"},而不是上传所有对象?
我试过 this.students.userName 但我收到错误 属性 'userName' does not exist on type '{ id: string;是编辑:布尔值;用户名:任意;用户生物:任何;个人资料图片:任何; }[]
您应该可以使用 this.students.userName
而不是 Object.values(this.students)
。
students:
{ id: string;
isEdit: boolean;
userName: any;
userBio: any;
profileImage: any; }[];
你确定你有这种结构吗,因为它包含很多错误 ;
分号在这里做什么以及 []
括号在这里扮演什么角色。
如果您的语法正确,请提供 students
的完整参考
我有一项服务可以提供这样的数据。我正在尝试构建评论功能。
students:
{ id: string;
isEdit: boolean;
userName: any;
userBio: any;
profileImage: any; }[];
现在我正在尝试获取 userName 的值,类似于 Object.values 但只有 userName。
addComment(){
let currentUser = firebase.auth().currentUser;
this.comments.push(this.comment);
let data = {
comments: firebase.firestore.FieldValue.arrayUnion
({comment: this.comment, username: Object.values(this.students) })
}
this.firebaseService.updatePost(this.item.id,data)
}
上面的代码导致了这个
我怎样才能上传用户名的值? 所以我可以{评论:"sdd",用户名:"snfs"},而不是上传所有对象?
我试过 this.students.userName 但我收到错误 属性 'userName' does not exist on type '{ id: string;是编辑:布尔值;用户名:任意;用户生物:任何;个人资料图片:任何; }[]
您应该可以使用 this.students.userName
而不是 Object.values(this.students)
。
students:
{ id: string;
isEdit: boolean;
userName: any;
userBio: any;
profileImage: any; }[];
你确定你有这种结构吗,因为它包含很多错误 ;
分号在这里做什么以及 []
括号在这里扮演什么角色。
如果您的语法正确,请提供 students