[英]How to iterate through a JSON response containing two objects in typescript?
I am exporting an interface from a separate typescript file. 我正在从单独的打字稿文件中导出接口。 While trying to iterate through the JSON response I am unable to parse it. 在尝试遍历JSON响应时,我无法解析它。 I need to separate out one of the objects from the JSON response and store it in another array. 我需要从JSON响应中分离出一个对象,并将其存储在另一个数组中。
This is the interface: 这是接口:
export interface IComplaintTagUri {
id: string
tag_uri: string
}
This is the typescript file where I am importing the interface: 这是我要导入接口的打字稿文件:
tagCategories: IComplaintTagUri[];
dropdown = [];
ngOnInit() {
this.tagCategories = [];
this.http.get<IComplaintTagUri[]>(tAPI.TagCategories).subscribe(result => {
this.tagCategories = result;
console.log("Result = ");
console.log(result);
console.log("Tag Categories = ");
console.log(this.tagCategories);
})
this.tagCategories.forEach(element => {
this.dropdown.push(element.tag_uri);
console.log(this.dropdown);
});
console.log(this.dropdown);
}
I am able to successfully store the result in tagCategories. 我能够将结果成功存储在tagCategories中。 But when I try to store the tag_uri in another array it does not work. 但是,当我尝试将tag_uri存储在另一个数组中时,它不起作用。
It seems as though the for loop is never accessed and the last log shows dropdown as an empty array. 似乎从未访问过for循环,并且最后一个日志将下拉列表显示为空数组。
Can anyone tell me where I'm going wrong? 谁能告诉我我要去哪里错了?
I tried including the forEach inside the subscribe function as well: 我也尝试将forEach包括在订阅函数中:
tagCategories: IComplaintTagUri[]; tagCategories:IComplaintTagUri []; dropdown = []; dropdown = [];
ngOnInit() {
this.tagCategories = [];
this.http.get<IComplaintTagUri[]>(tAPI.TagCategories).subscribe(result => {
this.tagCategories = result;
console.log("Result = ");
console.log(result);
console.log("Tag Categories = ");
console.log(this.tagCategories);
this.tagCategories.forEach(element => {
this.dropdown.push(element.tag_uri);
});
console.log(this.dropdown);
})
}
Now the dropdown list prints to the console as a series of undefined objects. 现在,下拉列表将作为一系列未定义的对象打印到控制台。
Subscribe
is an asynchronous function, the forEach
line will execute before the subscribe
block is complete, So, when forEach
is running, tagCategories
probably doesn't contain any values. Subscribe
是一个异步函数, forEach
行将在subscribe
块完成之前执行,因此,在运行forEach
时, tagCategories
可能不包含任何值。 Solution is to move forEach
inside the subscribe. 解决方案是将forEach
移到订阅中。
Second mistake you have is this.dropdown.push(element.taguri)
, it should be this.dropdown.push(element.tag_uri)
您this.dropdown.push(element.taguri)
第二个错误是this.dropdown.push(element.taguri)
,应该是this.dropdown.push(element.tag_uri)
this.http.get<IComplaintTagUri[]>(tAPI.TagCategories).subscribe(result => {
this.tagCategories = result;
console.log("Result = ");
console.log(result);
console.log("Tag Categories = ");
console.log(JSON.stringify(this.tagCategories));//make sure you have the result as you expect
this.tagCategories.forEach(element => {
this.dropdown.push(element.tag_uri);
console.log(this.dropdown);
});
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.