可以映射到另一个地图结果 rxjs 上吗?
Possible to map over another map result, rxjs?
我快 angular 7 了,在理解管道语法的工作方式时遇到了一些问题。
到目前为止,这是我的代码:
tests: Row[]
public getTest() : Observable<Row[]> {
return this.http.get<Test[]>(this.testUrl)
.pipe(
map((tests) => {
tests.map(test => test.rows = this.tests)
return this.tests;
}))
}
这是我的模型:
export interface Test {
total_rows: number;
offset: number;
rows: Row[];
}
export interface Row {
id: string;
key: Key;
value?: any;
}
export interface Key {
name: string;
}
这是 JSON :
{
"total_rows": 2,
"offset": 0,
"rows": [
{
"id": "54229d6897e1d1c7d603428a850081d5",
"key": {
"name": "test1"
},
"value": null
},
{
"id": "54229d6897e1d1c7d603428a85010e03",
"key": {
"name": "test2"
},
"value": null
}
]
}
我想要获取的数据是:行数组
感谢您的帮助
在您的示例中,一种方法属于 Observal.map()
and the other one is original native Javascript's array .map()
。
不确定您要实现什么,如果您只想检索 Rows
的数组并将其分配给 this.tests
,您可以简单地使用 Observable
转换 Observable.map()
并且不需要使用JS数组.map()
。记得订阅你的 observable:
public getTest() : Observable<Row[]> {
return this.http.get<Test[]>(this.testUrl)
.pipe(
map((tests) => tests.rows)))
//remember to subscribe
this.getTest().subscribe(results => this.tests = results) //assign the rows to local this.tests
我快 angular 7 了,在理解管道语法的工作方式时遇到了一些问题。
到目前为止,这是我的代码:
tests: Row[]
public getTest() : Observable<Row[]> {
return this.http.get<Test[]>(this.testUrl)
.pipe(
map((tests) => {
tests.map(test => test.rows = this.tests)
return this.tests;
}))
}
这是我的模型:
export interface Test {
total_rows: number;
offset: number;
rows: Row[];
}
export interface Row {
id: string;
key: Key;
value?: any;
}
export interface Key {
name: string;
}
这是 JSON :
{
"total_rows": 2,
"offset": 0,
"rows": [
{
"id": "54229d6897e1d1c7d603428a850081d5",
"key": {
"name": "test1"
},
"value": null
},
{
"id": "54229d6897e1d1c7d603428a85010e03",
"key": {
"name": "test2"
},
"value": null
}
]
}
我想要获取的数据是:行数组
感谢您的帮助
在您的示例中,一种方法属于 Observal.map()
and the other one is original native Javascript's array .map()
。
不确定您要实现什么,如果您只想检索 Rows
的数组并将其分配给 this.tests
,您可以简单地使用 Observable
转换 Observable.map()
并且不需要使用JS数组.map()
。记得订阅你的 observable:
public getTest() : Observable<Row[]> {
return this.http.get<Test[]>(this.testUrl)
.pipe(
map((tests) => tests.rows)))
//remember to subscribe
this.getTest().subscribe(results => this.tests = results) //assign the rows to local this.tests