gpt4 book ai didi

angular - filter(...).toArray 不是函数

转载 作者:太空狗 更新时间:2023-10-29 19:30:00 25 4
gpt4 key购买 nike

我正在尝试过滤订阅的结果。我从服务类 (job.service) 得到一个列表,所以我过滤它,但是过滤器返回一个 Iterable 而不是列表或数组。所以我使用 toArray() 但在运行时它抛出一个错误,说“result.filter(...).toArray 不是一个函数”。

有人可以解释发生了什么并给我一个解决方案吗?

import { Component, Input, OnInit } from "@angular/core";
import { Http, Response, Headers, RequestOptions, RequestMethod, Request } from "@angular/http";
import { JobBase,JobMore, Employee, WorkCategory, TimeEntryApi } from "./models";
import { JobService } from "./job.service";
import { EmployeeService } from "./employee.service";
import { WorkCategoryService } from "./workcategories.service";
import { Observable } from "rxjs/Observable";
import { List } from "immutable";
import "rxjs/add/operator/debounceTime";
import "rxjs/add/operator/distinctUntilChanged";
import "rxjs/add/operator/map";
import { NgbModal, NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";
dummy: Array<JobMore> = [];
getJobs(): void {
this.jobServ.GetJobs().subscribe(
result => this.dummy = result.filter(x => x.status > 9).toArray(),
error => console.error,
() => console.log('completed function here or remove '));

this.Submitted = this.dummy.length.toString();

}

工作服务

import { Component, Injectable } from "@angular/core";
import { Http, Response, Headers, RequestOptions, RequestMethod, Request } from "@angular/http";
import { JobBase, JobMore } from "./models";
import { Observable } from "rxjs/Observable";
import { BehaviorSubject } from "rxjs";
import { List } from "immutable";
import "rxjs/add/operator/debounceTime";
import "rxjs/add/operator/distinctUntilChanged";
import "rxjs/add/operator/map";
@Injectable()
export class JobService {

public jobs: List<JobMore>;

constructor(public http: Http) {
this.GetJobs();
}

GetJobs(): Observable<List<JobMore>>{
return this.http.get('./api/job')
.map(this.JobsResponseMapper);

}

JobsResponseMapper(response: Response) {
this.jobs = response.json();
return this.jobs;
}

}

模型

export class JobMore {
constructor(
public id: number,
public jobName: string,
public jobNumber: number,
public status: number,
) { }
}

预计到达时间:

我现在尝试了 result => Array.from(result.filter(x => x.status > 9)) 我收到以下错误“Argument of type

Iterable' is not assignable to parameter of type 'Iterable<{}>'. Property '[Symbol.iterator]' is missing in type 'Iterable'.

预计到达时间 2:

我现在已经尝试过,在 Array.from 中使用“this.dummy”。不过,这种语法对我来说没有任何意义。 dummy 是数组,Array.from 应该将列表转换为数组并返回它。然后应将其分配给虚拟对象。这没有道理吗?

getJobs(): void {
this.jobServ.GetJobs().subscribe(
result => {
Array.from(this.dummy = result.filter(x => x.status > 9)),
error => console.error,
() => console.log('jobs loaded: ' + this.jobs.length)
});
}

我也这样累过。同样的错误

getJobs(): void {
this.jobServ.GetJobs().subscribe(
result => {
this.dummy = Array.from(result.filter(x => x.status > 9)),
error => console.error,
() => console.log('jobs loaded: ' + this.jobs.length)
});
}

Iterable' is not assignable to parameter of type 'Iterable<{}>'. Property '[Symbol.iterator]' is missing in type 'Iterable'.

工作.服务

GetJobs() {
return this.http.get('./api/job')
.map(this.JobsResponseMapper);

}

最佳答案

如果这不是您想要的,我有点不确定为什么要在您的服务中将传入数据分配为 List?尝试从您的 GetJobs() 方法中删除 Observable,如下所示:

GetJobs() {
return this.http.get('./api/job')
.map(this.JobsResponseMapper);

}

但是对于您的问题,请尝试使用 Array.from 和几个括号:

getJobs(): void {
this.jobServ.GetJobs().subscribe(
result => {
Array.from(this.dummy = result.filter(x => x.status > 9)),
error => console.error,
() => console.log('completed function here or remove ')
this.Submitted = this.dummy.length.toString();
});
}

关于angular - filter(...).toArray 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41446512/

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