如何获取数组json而不是对象json

我做了一些自动完成功能,它需要一些数组方法。我想使用数组json作为我的API,而不是对象json。

因为过滤器方法仅适用于数组。我必须使用数组json []作为我的API网址,但是我的API是一个对象json {}文件。如何使其成为数组? 我尝试使用一些数组json API。该代码适用于数组json,但不适用于对象json。

HTML:

where

服务ts:

         <mat-autocomplete #auto="matAutocomplete" [displayWith]="getOptionText">
          <mat-option *ngFor="let option of (filteredOptions | async)" [value]="option">
            {{ option.make }}
          </mat-option>
         </mat-autocomplete>

组件ts:

@Injectable({
  providedIn: 'root'
})
export class Service {
  constructor(private http: HttpClient) { }

  opts = [];

  getData() {
    return this.opts.length ?
      of(this.opts) :
      this.http.get<any>('https://api.myjson.com/bins/15psn9').pipe(map(data => this.opts = data));
  }
}

我希望API JSON是数组或自动完成功能。

amianmian 回答:如何获取数组json而不是对象json

在您的服务中,尝试使用:from代替of。选中此docs

这是根据您的代码工作的stackblitz

本文链接:https://www.f2er.com/3164001.html

大家都在问