Skip to content
Advertisement

How to fetch data properly for this API? (array)

console.log screenshot

Hi, I am using “fetch” method with API for my website, and this API shows book information, if books include the input title.

Screenshot attached is console.log result when typing an example book title. I’d like to get every title info of each array, could anyone can help me on this?

Especially I am not sure what is the proper line for this.

JavaScript

Entire script for fetch is below.

JavaScript

Advertisement

Answer

You’re not calling the array index correctly, data[‘documents’.authors] should be data.documents[0].authors to get the author of the first item and data.documents[1].authors to get the second, etc…

What do you intend to do with the titles?

EDIT: Fixed it for easier use (I think this is what you want)

JavaScript

Otherwise create an array

JavaScript

push into array

JavaScript

But you might have issues with this and get empty array since the promise might still be pending

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement