How do I destructure this deep nested object?

I have this function below:

const displayUserPhotoAndName = (data) => {
    if(!data) return;

    // add your code here

    clearNotice();
  };

the data parameter is an API from https://randomuser.me/api/

The assignment has the instructions below:

Locate the displayUserPhotoAndName function and do the follwing within it:

Step 3 Still within the displayUserPhotoAndName function :

what I have done:

const displayUserPhotoAndName = (data) => {
    if(!data) return;

    // add your code here
    const {results} = data.results;
    const [profile] = results;
    const {title, First, Last} = results;
    const [,,,,,,,,,picture] = results;
    const largeImage = picture.large;
    userImage.src = largeImage;
    headerUserInfo.innerText = title + ' ' +  First + ' ' + Last;
    clearNotice();
    displayExtraUserInfo(profile);
  };

The error I get:

You have not de-structured the 'results' property from the 'data' parameter passed to 'displayUserPhotoAndName' function

I'm in dire need of assistance. Thanks in anticipation

Upvotes: 0

Views: 581

Answers (1)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

I'm not going to provide you the full answer but giving you the hints:

const { results } = data
const { profile } = results
console.log(profile)

Can be written as:

const { results: { profile } } = data
console.log(profile)

Here are my some posts from which you may go further:

destructure an objects properties

how is this type annotation working

why source target when destructuring

Upvotes: 3

Related Questions