简体   繁体   English

从 Api 循环数据

[英]Looping the the data from an Api

I have an api from which I want to display the data, I have 2 arrays each one has a category how would I iterate and display these 2 categories, so category Arts and category Entertainment.我有一个 api 我想从中显示数据,我有 2 个 arrays 每个都有一个类别,我将如何迭代和显示这两个类别,所以类别艺术和娱乐类别。 I have tried something but it is not working it is giving me this error TypeError: Cannot read property 'map' of undefined would really appreciate the help.我已经尝试了一些东西,但它不起作用它给了我这个错误TypeError: Cannot read property 'map' of undefined非常感谢帮助。 Thanks in advance提前致谢

import React,{useState, useEffect} from 'react'
import './Home.css'
import Books from './Books'
const url="https://json-api-smaiil.herokuapp.com/books"


const Home = () => {

  const [loading, setLoading] = useState(true)
  const [books, setBooks] = useState({})

    const fetchData =async()=>{
    setLoading(true)
    const response = await fetch(url)
    const data = await response.json()
    console.log(data)
    setBooks(books)
    }

  useEffect(()=>{
    fetchData();
  })
  

    return (
        <div>
          <h1 className='categories'>Categories</h1>
          {books[0].map((book)=>{
            return (<li key={book.id}>{book.category}</li>)
          })}
        </div>
    )
}

export default Home

{
     "Arts":[{
    "category": "Arts"
    Language: "English"
    Narrated by: "Faith G. Harper PhD LPC-S ACS ACN"
    Regular price: "$17.47"
    Release date: "03-20-18"
    bookName: "Unf--k Your Brain"
    by: "Faith G. Harper PhD LPC-S ACS ACN"
    category: "Arts and Entertainment"
    id: "1"
},
{

    Language: "English"
    Length: "1 hr and 33 mins"
    Narreted by: "James Taylor"
    Regular price: "$9.95"
    Release date: "01-31-20"
    Series: "Words + Music"
    bookName: " Break Shot: My First 21 Years"
    by: "James Taylor"
    id: "2"
}],

    "Entertainment":[{   
    "category" :"Entertainment"
    "id": "9",
    "image": "https://m.media-amazon.com/images/I/51tpSb0iy+L._SL500_.jpg",
    "bookName": "The Hiding Place",
    "by": "Corrie ten Boom, John Sherrill, Elizabeth Sherrill",
    "Narreted by": "Wanda McCaddon",
    "Length": "8 hrs and 14 mins",
    "Release date": "10-03-11",
    "Language": "English",
    "rating": "6,641 ratings",
    "Regular price": "$24.95",
  
},

{
    Language: "English"
    Length: "1 hr and 7 mins"
    Narreted by: "Aidan Gillen"
    Regular price: "$9.95"
    Release date: "03-31-15"
    bookName: "The Art of War"
    by: "Sun Tzu"
    id: "12"   
    rating: "19,765 ratings"
 
}]
}

change this.改变这个。 cause you have to map object, not array.因为你必须 map object,而不是数组。

books[0].map((book)=>{
     return (<li key={book.id}>{book.category}</li>)
})

to

Object.keys(books).map(function(key, index) {
  let book = books[key];
  return <li key={key}>{book[0].category}</li>
});

try the below code:试试下面的代码:

      books.Arts.map((book)=>{
        return (<li key={book.id}>{book.category}</li>)
      })

One issue I see is the entry at index 1 of your array doesn't have key value corresponding to category which would give you undesired results.我看到的一个问题是您的数组索引 1 处的条目没有与类别对应的键值,这会给您带来不希望的结果。

const [books, setBooks] = useState({})

At this line you are setting the default value as an Object you are getting that error because the map function does not work with an object. At this line you are setting the default value as an Object you are getting that error because the map function does not work with an object. Change the default to an empty array .将默认值更改为空数组

const [books, setBooks] = useState([])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM