【发布时间】:2020-07-21 10:51:32
【问题描述】:
我正在尝试获取存储库列表,即我的代码使用过滤器搜索存储库
Javascript 得到一个结果,其中包含多个项目,其中包含每个存储库的数据,这些数据使用 URL:https://api.github.com/search/repositories?q=piccolowen+in:name 进行过滤。
我可以使用console.log(result.items[0].name) 来获取第一个存储库的name 值,但我想从打印到控制台的搜索中获取所有存储库。我还希望代码能够打印所有存储库及其值,无论有多少存储库适合过滤器。
这是我要添加到的当前代码:
window.onload = func()
async function func() {
const url = 'https://api.github.com/search/repositories?q=piccolowen+in:name'
const response = await fetch(url);
const result = await response.json();
const apiresult = document.getElementById('thisisanid')
console.log(result)
}
关于如何做到这一点的任何想法?
编辑: 我使用这个问题的while循环找到了我的问题的答案:Get total number of items on Json object?
const resultLength = Object.keys(result.items).length
var arrnum = 0
while (arrnum < resultLength) {
//execute code
}
编辑 2: 我之前编辑的代码会使页面崩溃。仍在为这个巨大的错误寻找解决方案。
【问题讨论】:
标签: javascript async-await github-api