【发布时间】:2020-12-07 06:03:19
【问题描述】:
如何在 typescript 中打印数组中的特定值?我在打字稿中有以下代码:
import { AngularFirestore } from '@angular/fire/firestore';
export class ProfileComponent implements OnInit {
myArray: any[] = [];
constructor(public afs: AngularFirestore) {}
emailtoprint = "sadasd@assf.co";
test = this.afs.collection('doctors').get().subscribe((ss) => {
ss.docs.forEach((doc) => {this.myArray.push(doc.data()); });
});
}
emailtoprint 的电子邮件以 html 格式打印,如下所示:
<div *ngFor='let i of myArray'>
<div *ngIf="i.email == emailtoprint">{{i.email}}</div>
</div>
如您所见,我可以使用 ngIf 找到相应的电子邮件...虽然,如果 myArray 有很多值,这并不是最佳选择。我想知道如何从打字稿角度的数组中获取特定值?我尝试了以下方法,但得到一个空输出:
test3 = this.myArray.find(e => e.email === emailtoprint);
当我这样做时,这是为 myArray 找到的实体
<li *ngFor="let i of myArray">
{{i | json}}
</li>
{ "firstName": "Df", "lastName": "Sdf", "email": "sadasd@assf.co"}
{ "firstName": "Anna", "lastName": "Sims", "email": "doctor@hotmail.com"}
{ "firstName": "John", "lastName": "Adad", "email": "nurse@hotmail.com" }
【问题讨论】:
-
我们可以知道您的 this.myArray 中的示例数据吗? :) 由于您的 .find() 是正确的,而且我认为 this.myArray 数据中的某些内容可能会因其结构而被忽略
-
我更新了我的问题以显示我的数组数据。从另一个角度来看,我认为最好只能使用 emailtoprint 获取数据库中的列,当我执行
test = this.afs.collection('doctors').get().subscribe((ss) => { ss.docs.forEach((doc) => {this.myArray.push(doc.data()); }); });但我不知道该怎么做时 -
您需要
myArray提供的所有信息吗? -
我只需要sadasd@assf.co的专栏
-
我尝试了以下但它打印 [Object object]
test2 = this.afs.collection('doctors', ref => ref.where('email', '==', this.email));
标签: arrays angular typescript