gpt4 book ai didi

javascript - 如何显示 Github 存储库的贡献者

转载 作者:行者123 更新时间:2023-12-04 07:19:15 27 4
gpt4 key购买 nike

关闭。这个问题需要details or clarity .它目前不接受答案。












想改善这个问题吗?通过 editing this post 添加详细信息并澄清问题.

27 天前关闭。




Improve this question




我是 HTML CSS 和 js 的初学者,我正在尝试列出 Github 中存储库的贡献者
我已经使用 API 收集了 JSON 对象
https://docs.github.com/en/rest/reference/repos#list-repository-contributors
但我不知道如何在 div 中使用 HTML 列出所有这些
看看我的js文件:

const getUsers = () => {
axios
.get(
"https://api.github.com/repos/{owner}/{repo}/contributors "
)
.then((response) => {
const users = response.data;
console.log(users);
const h1 = document.querySelector(".name");
h1.innerHTML = `${users[1].login}`;
})
.catch((error) => console.error(error));
};
getUsers();

这是我的数据对象的示例:

[
{
"login": "bugron",
"id": 13225220,
"node_id": "MDQ6VXNlcjEzMjI1MjIw",
"avatar_url": "https://avatars.githubusercontent.com/u/13225220?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bugron",
"html_url": "https://github.com/bugron",
"followers_url": "https://api.github.com/users/bugron/followers",
"following_url": "https://api.github.com/users/bugron/following{/other_user}",
"gists_url": "https://api.github.com/users/bugron/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bugron/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bugron/subscriptions",
"organizations_url": "https://api.github.com/users/bugron/orgs",
"repos_url": "https://api.github.com/users/bugron/repos",
"events_url": "https://api.github.com/users/bugron/events{/privacy}",
"received_events_url": "https://api.github.com/users/bugron/received_events",
"type": "User",
"site_admin": false,
"contributions": 297
}
]

我想获取登录值并将它们显示在不同的 div 中

最佳答案

您可以使用 array#map并提取所有 login并使用 array#join ,你可以加入这些login .

const getUsers = () => {
const owner = 'fabpot',
repo = 'symfony';
axios
.get(
`https://api.github.com/repos/${owner}/${repo}/contributors`
)
.then((response) => {
const users = response.data;
const div = document.querySelector(".name");
div.innerHTML = users.map(u => u.login).join('<br />');
})
.catch((error) => console.error(error));
};
getUsers();
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script>
<div class="name"></div>

关于javascript - 如何显示 Github 存储库的贡献者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68607241/

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