4

I have an object "data" from 'https://randomuser.me/api/' which I'm using in my project.

{
  "results": [
    {
      "gender": "female",
      "name": {
        "title": "miss",
        "first": "mia",
        "last": "sutton"
      }
    }
  ]
}

I destructured results from the data object as follows; const {results} = data; How do I destructure the results variable I created, and obtain the first item from it I want the de-structured array item to be declared as profile. This represents the profile data for the user gotten from the API call that I want to display in my app.

7
  • 2
    const {results: [{name: {title, first, last}}]} = data; This will completly destructure the first item on your list. Commented Mar 14, 2019 at 10:05
  • 1
    Thank you. How about if I want to give the first item in the array a name. For example myArray. So when I log myArray, I should get; ` { "gender": "female", "name": { "title": "miss", "first": "mia", "last": "sutton" }` Commented Mar 14, 2019 at 10:10
  • @Gm Emmy, this my answer below, I give an example of how to destructure the first item in the array, and go deeper if you need to Commented Mar 14, 2019 at 10:11
  • 1
    @GmEmmy Jo_va gave some examples for destructuring your object. If you just want to have your first element in an variable, simply use const {results: [firstPerson]} = data; Commented Mar 14, 2019 at 10:13
  • @Seblor Does the firstPerson represent the array item's new variable name? Commented Mar 14, 2019 at 10:20

1 Answer 1

8

You can do it like this:

const { results: [firstItem] } = data;

See this MDN article for more info on destructuring.

const data = {
  "results": [
    {
      "gender": "female",
      "name": {
        "title": "miss",
        "first": "mia",
        "last": "sutton"
      }
    }
  ]
};

// declare a const variable named firstItem that holds the first element of the array
const { results: [firstItem] } = data;
// You could event destructure the content of this first array item like this
const { results: [{ gender, name }] } = data;
// or go deeper like this
const { results: [{ name: { title, first, last } }] } = data;

console.log(firstItem);
console.log(gender);
console.log(name);
console.log(title, first, last);

According to your code (see comments), this should work too:

const data = {
  "results": [
    {
      "gender": "female",
      "name": {
        "title": "miss",
        "first": "mia",
        "last": "sutton"
      }
    }
  ]
};

const displayUserPhotoAndName = (data) => {
  if(!data) return;
  const { results } = data;
  const { results: [profile] } = data;

  console.log(profile);
}

displayUserPhotoAndName(data);

Sign up to request clarification or add additional context in comments.

4 Comments

@Gm Emmy, is this example working for you ? const { results: [firstItem] } = data;
No it's not unfortunately.
` const displayUserPhotoAndName = (data) => { if(!data) return; const {results} = data; const {results: [profile]} = data;}` I'm doing it inside a function that receives data as it's parameter.
@Gm Emmy, your error must be somewhere else, there is nothing wrong with that code. I updated the answer and added a snippet with your code and it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.