0

I would like to sort out some data from a Fetch API JSON. Using Postman GET is fine, but when I use console.log() to print out the result, it has the [Array] printed out instead. Also, I cannot access the "foodNutrients" array in my code.

When I enter the url using Postman GET

"foodNutrients": [
                {
                    "nutrientId": 1051,
                    "nutrientName": "Water",
                    "nutrientNumber": "255",
                    "unitName": "G",
                    "derivationCode": "A",
                    "derivationDescription": "Analytical",
                    "value": 0E-8
                },
                {
                    "nutrientId": 1003,
                    "nutrientName": "Protein",
                    "nutrientNumber": "203",
                    "unitName": "G",
                    "derivationCode": "A",
                    "derivationDescription": "Analytical",
                    "value": 24.40000000
                },
               ]

But when I use Fetch and use console.log()

  {
    //some fields

    foodNutrients: [Array]
  },

my code

const dataFilter= (response) => {
    console.log(response)

 async function getData() {
    fetch('https://api.nal.usda.gov/fdc/v1/foods/search?api_key=DEMO_KEY&query=0')
      .then(response => response.json())
      .then(response => dataFilter(response))
  }
  getData()

Thanks for the help :)

3
  • its an object in postman and in console.log both i don't what are you trying to ask exactly Commented Jun 24, 2020 at 12:23
  • It's an array in both Commented Jun 24, 2020 at 12:24
  • What do you get when you do console.log(JSON.stringify(response, null, 2))? Commented Jun 24, 2020 at 12:24

1 Answer 1

2

This is just how the Console expresses "There is a large array here".

It's a performance feature which works by simplifying the view on the data.

You could, for example, change console.log(response) to response.foodNutrients.forEach(nut => console.log(nut)) will show the objects in the array.

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

1 Comment

Ah. I should also be able to access the data inside it right?

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.