【问题标题】:Angular fetch data from API, want to only fect the arrayAngular 从 API 获取数据,只想影响数组
【发布时间】:2020-08-31 07:58:53
【问题描述】:

我在 Angular 项目中从 API 获取数据。我收到很多数据,但想选择数据获取。我只需要获取“结果”数组。我不知道我必须在我的函数中做什么。

This is all data fetch with my function

This is my functionng

如果你有什么建议,请告诉我。

坦克

【问题讨论】:

  • 好像你的 API 是一个 REST API,所以 API 的响应是由你的项目的后端而不是前端决定的。实际上 GraphQL 为您提供了这个功能,但现在情况不同了。您必须检查您的后端 API 是否支持此类行为。
  • 看起来您是这里的新贡献者,欢迎您!您能否提供内嵌代码示例而不是图像链接?有关示例和帮助,请参阅 Code Formatting Help

标签: arrays json angular api fetch


【解决方案1】:

如果我猜对了,你想做的是:

import {map} from 'rxjs/operators';

...

searchMovies() {
  return this.http.get(...)
      .pipe(map((response: any) => response.results));
}

如果你使用的 rxjs 库的版本是

import 'rxjs/add/operator/map';

...

searchMovies() {
  return this.http.get(...)
      .map((response: any) => response.results);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2018-11-11
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2019-03-22
    相关资源
    最近更新 更多