[英]Using fetch and randomuser.me, how do I return multiple results?
I'm trying to use randomuser.me and I have the fetch request set up properly.我正在尝试使用 randomuser.me 并且正确设置了获取请求。 I'm returning a single user.
我要返回一个用户。 However, I want to return 5 comma separated users.
但是,我想返回 5 个逗号分隔的用户。
According to randomuser.me's documentation , I only need to append the fetch URI with ?results=5
(or any number I choose) and the multiple users are returned.根据randomuser.me 的文档,我只需要 append 获取 URI 与
?results=5
(或我选择的任何数字)并返回多个用户。
I've done this in the snippet below, but I'm still only returning a single user.我已经在下面的代码段中完成了这项工作,但我仍然只返回一个用户。
How do I return a comma separated result of 5 users?如何返回 5 个用户的逗号分隔结果?
window.onload = () => { randomUserGenerator(); }; const randomUserGenerator = () => { fetch("https://randomuser.me/api/?results=5").then((res) => { return res.json(); }).then((data) => { showRandomUserData(data); }); }; showRandomUserData = (randomUser) => { document.getElementById("name").innerText = `${randomUser.results[0].name.first} ${randomUser.results[0].name.last}`; };
<h3><u>Users:</u> <span id="name"></span></h3>
using results[0]
... you're only USING the first user using
results[0]
...您只使用第一个用户
Something like this may help you along这样的事情可能会帮助你
window.onload = () => { randomUserGenerator(); }; const randomUserGenerator = () => { fetch("https://randomuser.me/api/?results=5").then((res) => { return res.json(); }).then((data) => { showRandomUserData(data); }); }; showRandomUserData = (randomUser) => { // combine all users const users = randomUser.results.map(({name: { first, last}}) => `${first} ${last}`).join(', '); // show them document.getElementById("name").innerText = users; };
<h3><u>Users:</u> <span id="name"></span></h3>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.