简体   繁体   English

使用 Map() function 提取带有 ReactJS 的双嵌套 JSON 数据

[英]Using Map() function to pull out double nested JSON data with ReactJS

I'm trying to pull out the individual images in the "images" object below under "details" section.我正在尝试在“详细信息”部分下的“图像”object 中提取单个图像。

Seem to just be getting nothing printing out.似乎只是没有打印出来。 Looking for the correct way to pull within the details.images.image1,2, or 3.寻找在 details.images.image1、2 或 3 中提取的正确方法。

Here is the JSON data I'm working with so far:这是我目前正在使用的 JSON 数据:

{
  "books": [
    {
      "title": "title 1",
      "image": "/image1.jpg"
    },
    {
      "title": "title 2",
      "image": "/image2.jpg"
    }
  ],
  "details": [
    {
      "author": "book author",
      "name": "Book name",
      "price": 34.99,
      "publisher": "Penguin Books",
      "images": [
        {
          "image1": "/image1.jpg",
          "image2": "/image2.jpg",
          "image3": "/image3.jpg"
        }
      ]
    }
  ]
}

Also here is the JSON call I'm making in a Book component:这也是我在 Book 组件中进行的 JSON 调用:

{staticdata.details.map(detail => (
  <Book
    book_name={detail.author}
    book_price={detail.price}
    image={detail.images.image1}
  />
))}

Here's an example of accessing those nested properties and logging them to the console.这是访问这些嵌套属性并将它们记录到控制台的示例。 It appears your attempt was mostly correct, but images is an array.看来您的尝试大部分是正确的,但images是一个数组。

 const data = { "books": [ { "title": "title 1", "image": "/image1.jpg" }, { "title": "title 2", "image": "/image2.jpg" } ], "details": [ { "author": "book author", "name": "Book name", "price": 34.99, "publisher": "Penguin Books", "images": [ { "image1": "/image1.jpg", "image2": "/image2.jpg", "image3": "/image3.jpg" } ] } ] } data.details.map(detail => { console.log(detail.author, detail.price, detail.images[0].image1); });

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

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