我想获取API并对其进行解构

问题描述 投票:0回答:1

我想获取此API:

const getAUserProfile = () => {
  const api='https://randomuser.me/api/';

  fetch('https://randomuser.me/api/').then(resonse => response.json()) // make API call here

并使用这些功能对其进行解构,将位置,出生日期和电话号码分配给以下功能:

const displayBirthdate = () => {}
const displayPhone = () => {}
const displayAddress = () => {}

请有人帮帮我吗?

javascript fetch-api
1个回答
0
投票

您的回复中有一个拼写错误,我认为您应该更多地进行解构。 Check out this page.

在一个句子中,解构将值从对象或数组赋值给变量。

const getAUserProfile = () => {
  const api = 'https://randomuser.me/api/';
  console.log("boop");
  return fetch(api)
    .then((response) => {

      return response.json();

    });
}
getAUserProfile().then((result) => {

  console.log(result.results[0]);

  //note the object has values dob, phone and location
  let {
    dob,
    phone,
    location
  } = result.results[0]; //Destructuring assignment

  displayBirthdate(dob);
  displayBirthdate(phone);
  displayAddress(location);
});

function displayBirthdate(dob) {
  console.log(dob);
}

function displayBirthdate(phone) {
  console.log(phoneb);
}

function displayAddress(location) {
  console.log(location);
}
© www.soinside.com 2019 - 2024. All rights reserved.