gpt4 book ai didi

javascript - 获取数组对象类型脚本中的第一个索引

转载 作者:行者123 更新时间:2023-11-30 14:00:11 25 4
gpt4 key购买 nike

我想在下面的代码片段中从数组对象 dataList 获取索引零记录。我是 JavaScript 和 Typescript 的新手。

dataList: Array<any> = [];
data: any;

constructor(private apiService: ApiService) {


this.apiService.getEncounterDashBoard(this.searchCond)
.subscribe(data => {
this.dataList = data.result;
});

let labels = this.dataList[0];
}

最佳答案

你可以这样试试

dataList: Array<any> = [];
data: any;

constructor(private apiService: ApiService) {

let labels = [];
this.apiService.getEncounterDashBoard(this.searchCond)
.subscribe(data => {
this.dataList = data.result;
labels = this.dataList[0];
// here you can see that we are access the data inside the subscription block bcs javascript is asynchronous. So it is not wait complete the api call
});
}

关于javascript - 获取数组对象类型脚本中的第一个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56440939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com