[英]Angular2: How can i iterate each object to an observable containing object array data?
Hi guys i am just starting to learn(upgrade to be specific) Angular 2, i just have some few questions. 嗨,大家好,我刚刚开始学习(具体升级)Angular 2,我有几个问题。 how do i iterate each object in the observable of array object? 我如何迭代数组对象的可观察对象?
this is what i retrieve if i go get data from "api/v1/example" already postman on this and yes its working. 这是我检索的内容,如果我要从已经有“邮递员”的“ api / v1 / example”中获取数据,并且可以正常工作。 . 。
// my arrayObject
[
{
"userID": 12,
"username": "admin1",
"password": "admin2"
},
{
"userID": 13,
"username": "admin21",
"password": "admin32"
},
{
"userID": 14,
"username": "admin221",
"password": "admin72"
},
{
"userID": 15,
"username": "admin451",
"password": "admin652"
},
{
"userID": 16,
"username": "admin561",
"password": "admin1222"
},
{
"userID": 17,
"username": "admin154",
"password": "admin572"
},
{
"userID": 127,
"username": "admin1254",
"password": "admin5721"
}
]
So as what i said before i'm just learning angular 2, so i tried to use this dummy data to test observable, but somehow in my codes i can't really iterate each data in the object, this is what i tried so far 因此,正如我在刚刚学习角度2之前所说的那样,我尝试使用此虚拟数据来测试可观察性,但是以某种方式在我的代码中,我无法真正迭代对象中的每个数据,这是我到目前为止所尝试的
myData: Any[];
constructor(private _http: HttpClient) { }
ngOnInit() {
this._http.get<Any[]>("api/v1/example")
.map(data => data["username"])
.subscribe(data => console.log(data));
}
but i don't get any data, i even tried the filter 但我没有任何数据,我什至尝试了过滤器
filter(data => data["id"] > 0)
but still fails, but if i just subscribe immediately like this i can retrieve the data 但仍然失败,但是如果我像这样立即订阅,我可以检索数据
ngOnInit() {
this._http.get<Any[]>("api/v1/example")
.subscribe(data => console.log(data));
}
i use the stream in java as reference for this but i think they don't work the same, thank you in advance guys 我使用java中的流作为参考,但是我认为它们的工作原理不同,在此先感谢您
Try below code: 试试下面的代码:
import { Observable } from 'rxjs/Observable';
import { from } from 'rxjs/observable/from';
import { of } from 'rxjs/observable/of';
import { map } from 'rxjs/operators';
this._http.get<Any[]>("api/v1/example").pipe(
map(d => { return d["username"]; })
).subscribe(data => console.log(data));
refer working stackblitz 引用工作stackblitz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.