我有一个带有嵌套数组的接口:
import { IEmitente } from './ICustomer';
export interface IAccounts {
current_page: number;
data: ICustomer[];
first_page_url: string;
from: number;
last_page: number;
last_page_url: string;
next_page_url: string;
path: string;
per_page: number;
prev_page_url: string;
to: number;
total: number;
}
客户界面如下:
export interface ICustomer {
id: number;
name: string;
federalId: string;
firstBuy: date;
balance: number;
}
我在使用此http get方法:
getContas(identific,search,page?): Observable<IAccounts[]> {
return this.httpClient.get<IAccounts[]>(`${this.BASE_URL}/getContas?identific=${identific}&search=${search}&page=${page}`)
}
可观察到的是这样的:
{current_page: 1, data: Array(15), first_page_url: "https://web.gruposol.com.br/endpoint/api/getContas?page=1", from: 1, last_page: 19, …}
current_page: 1
data: (15) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
first_page_url: "https://address/api/getContas?page=1"
from: 1
last_page: 19
last_page_url: "https://address/api/getContas?page=19"
next_page_url: "https://address/api/getContas?page=2"
path: "https://address/api/getContas"
per_page: 15
prev_page_url: null
to: 15
total: 275
__proto__: Object
如何使用rxjs从可观察的数据中仅获取数据(客户)部分?
我需要这样的东西:
在变量customers:ICustomer[] = [];
上
我只想接收来自Observable的数据数组。我怎样才能做到这一点?
当前我的ngOnInit方法是这样的:
ngOnInit() {
this.AlvosService.getContas(this.identific, this.search)
.subscribe((response) => { console.log(response)} ),
(error) => { console.log(error); };
}
答案 0 :(得分:1)
您可以使用管道和地图嵌套值
var imageIndexes = [0,1,2,3,4,5,6,7,8,9,10,11]
function displayImage() {
var index = Math.floor(Math.random() * (imageIndexes.length));
var num = imageIndexes[index]
var result = imagesArray[num];
imageIndexes.splice(index, 1)
if (imageIndexes.length === 0) {
imageIndexes = [0,1,2,3,4,5,6,7,8,9,10,11]
}
return result
}
function changeImagesSrc() {
$(".deluxe_img").each(function () {
$(this).attr('src',displayImage())
})
}
$(document).ready(function () {
changeImagesSrc()
setInterval(function() {
changeImagesSrc()
}, 5000);
})
注意:您最好不要使用以大写字母开头的变量,如 AlvosService 那样,这是不正确的做法
答案 1 :(得分:0)
尝试一下:
var teamPrincipalTypeCode = 9;
var query = new QueryExpression("task");
var principalObjectAccess = query.AddLink("principalobjectaccess", "activityid", "objectid");
principalObjectAccess.LinkCriteria.AddCondition("principaltypecode", ConditionOperator.Equal, teamPrincipalTypeCode);
var entityCollection = _service.RetrieveMultiple(query);
由于您的http调用返回的是this.AlvosService.getContas(this.identific, this.search)
.pipe(
mergeMap(accounts => accounts),
map((account: IAccounts) => account.data),
toArray()
)
.subscribe((customers) => { console.log(customers)} ),
(error) => { console.log(error); };
数组,因此您需要IAccounts
数组,flatten
数据并将map
再次放入数组。
答案 2 :(得分:0)
我弄清楚了错误所在。
如果您在上面查看响应,您会看到响应对象不是数组。
问题是服务中获取方法的括号。只能使用<IAccounts[]>
代替<IAccounts>
。因此,更改了这样的方法:
getContas(identific,search,page?): Observable<IAccounts> {
return this.httpClient.get<IAccounts>(`${this.BASE_URL}/getContas?identific=${identific}&search=${search}&page=${page}`)
}
有效! 非常感谢您的帮助。