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

时间:2019-04-09 00:34:49

标签: javascript fetch-api

我想获取此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 = () => {}

请有人帮我吗?

1 个答案:

答案 0 :(得分: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);
}