简体   繁体   English

使用 fetch 和 randomuser.me,我如何返回多个结果?

[英]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.

相关问题 CORS 政策已阻止从源“http://localhost:3000”获取“https://randomuser.me/api/?results=4”的访问权限: - Access to fetch at 'https://randomuser.me/api/?results=4' from origin 'http://localhost:3000' has been blocked by CORS policy: 我想从 api 获取数据(姓名和电子邮件)。 我的程序已成功编译,但我在控制台中遇到错误。 https://randomuser.me/api - I am suppose to fetch data (name & email) from api. My program is compiled sucessfully but I'm getting errors in console. https://randomuser.me/api 从randomUser.me API输出大写名称? - Output capitalized names from randomUser.me api? 获取随机用户 API 结果 - Fetch randomuser API results 从randomuser.me给定的JSON获取值并循环遍历以在div中追加 - get values from JSON given by randomuser.me and loop through to append in divs 分解API https://randomuser.me/api/以获取标题,姓氏和名字,以及API返回的用户个人资料的大图片 - Destructuring the API https://randomuser.me/api/ to grab title, last name, and first name also large photo of the user profile returned by the API 如何使用 reactjs 从多个 API 获取搜索结果 - How do I fetch search results from multiple API using reactjs 如何使用多个函数对提取的结果求和? - How to sum the results of a fetch using multiple functions? 如何使用Sequelize和Javascript返回查询结果? - How do I return the results of a query using Sequelize and Javascript? 如何使用jQuery过滤多个选择组的结果? - How do I filter results with multiple select groups using jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM