You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从GitHub API获取指定仓库属性而非全部属性?

提取GitHub搜索API返回的指定属性

解决办法很直接:拿到API返回的数据后,对仓库列表数组做属性筛选,只保留你需要的字段就行。另外注意原代码里的{json: true}是无效配置,fetch并没有这个参数,得用response.json()来解析响应体。

修改后的代码如下:

const response = await fetch(
  "https://api.github.com/search/repositories?q=calculator&per_page=100&type=all&language=&sort=stargazers"
);
const data = await response.json();

// 筛选出需要的属性
const filteredRepos = data.items.map(repo => ({
  id: repo.id,
  name: repo.name,
  description: repo.description,
  html_url: repo.html_url,
  updated_at: repo.updated_at,
  stargazers_count: repo.stargazers_count,
  forks_count: repo.forks_count
}));

console.log(filteredRepos);

这段代码会遍历API返回的所有仓库项,每个项只保留你指定的那几个属性,最终得到一个仅包含目标字段的新数组。

内容的提问来源于stack exchange,提问作者AlexUA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 19:10:19