Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

reactjs - How to read a nested json?

I don't see any mistake with the following code but yet still I am getting error message "item._highlightResult.map is not a function".

    {items.map((item, index) => (
<li key={index}><a href={item.story_url} target="_blank">
 {(item._highlightResult.map)(sub => {
     {sub.story_title.map (inhere => {
         {inhere.value}
     })}
 })} 

and this is my json data: https://hn.algolia.com/api/v1/search_by_date?query=world

How can I make this work?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

map() is the method of Array, but _highlightResult is the object. You should use Object.keys() to iterate the object by key.

Object.keys(item._highlightResult).map(key => {
  // Use `item._highlightResult[key]`
})

story_title is either string or object, so you should check it before using Object.keys().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...