简体   繁体   English

React.js 映射 json object 与多个嵌套 arrays; 迭代数据并传递到轮播

[英]React.js mapping json object with multiple nested arrays; iterating the data and passing into a carousel

I'm having a hard time reaching wanted data from my json.我很难从我的 json 中获得想要的数据。

Since I can't post original one, I'll make an example因为不能发原创,我举个例子

{ example1: "some string",
example2: [{
     data:{
      category:{
        categoryName: "some name",
        subCategory:[{
            id: 123
            name: "another name",
            info: {id: "1",title: "title"}
            thefinal:[{
                 id: "12323",
                 datetime: {date: "24\/01\/21",time: "22:20"}
            }]
        }]
      }
  }
}]
  }

Now, I can fetch the data up to some point and print it but I think I'm doing it the wrong way.现在,我可以获取数据并打印出来,但我认为我做错了。 When I fetch the data, I save the result as result.example2.当我获取数据时,我将结果保存为 result.example2。

  const[example, setExample] = useState([])

  useEffect(() => {
   const fetchExamples = async () => {
    
    const result = await axios(url)

    setExample(result.data.example2)
 
   }
   fetchExamples()
  }, )

Then I pass it into another component where I made a variable for some of these parameters;然后我将它传递给另一个组件,在那里我为其中一些参数创建了一个变量; such as let statusId = categoryName.subCategory[0].thefinal[0].id // When I console statusId it shows well, but I need a way of making this less static.比如 let statusId = categoryName.subCategory[0].thefinal[0].id // 当我控制台 statusId 它显示得很好,但我需要一种方法来减少这个 static。 I've also made a map我还做了一个 map

   <div>  {subCategory.slice(0, max).map((item, index) => <div key={index}>{subCategory.item}</div>)}</div>

It successfully shows a list of ids, even though I won't need them in such format.它成功地显示了一个 id 列表,即使我不需要这种格式的它们。 So I can access the data, just not the right way.所以我可以访问数据,只是方式不对。

Do I need to do multiple maps, one after the other?我需要一张接一张地做多张地图吗?

PT.2 I eventually need to pass this data from different arrays into a carousel. PT.2 我最终需要将这些数据从不同的 arrays 传递到轮播中。 I also managed to print all the values for specific parameters but I would have to show one at a time.我还设法打印了特定参数的所有值,但我必须一次显示一个。 For example I'll have 5 slides;例如,我将有 5 张幻灯片; 5 carousel items that show only one id at a given slide (slide 1: id 1; slide 2: id:2) 5 个轮播项目,在给定幻灯片上仅显示一个 id(幻灯片 1:id 1;幻灯片 2:id:2)

I'm sorry if it doesn't make much sense, I could try to give more info or maybe even sanbox of some sort.很抱歉,如果它没有多大意义,我可以尝试提供更多信息,或者甚至是某种类型的 sanbox。 I've spent so much time trying to figure this out that I can't think clearly anymore and I have nobody to ask.我花了很多时间试图弄清楚这一点,以至于我无法再清楚地思考,也没有人可以问。 It may seem as an easy task but I'm just a beginner and it's overwhelming.这似乎是一项简单的任务,但我只是一个初学者,而且它是压倒性的。

I'm having a hard time reaching wanted data from my json.我很难从我的 json 中获得想要的数据。

Since I can't post original one, I'll make an example因为不能发原创,我举个例子

{ example1: "some string",
example2: [{
     data:{
      category:{
        categoryName: "some name",
        subCategory:[{
            id: 123
            name: "another name",
            info: {id: "1",title: "title"}
            thefinal:[{
                 id: "12323",
                 datetime: {date: "24\/01\/21",time: "22:20"}
            }]
        }]
      }
  }
}]
  }

Now, I can fetch the data up to some point and print it but I think I'm doing it the wrong way.现在,我可以获取数据并打印出来,但我认为我做错了。 When I fetch the data, I save the result as result.example2.当我获取数据时,我将结果保存为 result.example2。

  const[example, setExample] = useState([])

  useEffect(() => {
   const fetchExamples = async () => {
    
    const result = await axios(url)

    setExample(result.data.example2)
 
   }
   fetchExamples()
  }, )

Then I pass it into another component where I made a variable for some of these parameters;然后我将它传递给另一个组件,在那里我为其中一些参数创建了一个变量; such as let statusId = categoryName.subCategory[0].thefinal[0].id // When I console statusId it shows well, but I need a way of making this less static.比如 let statusId = categoryName.subCategory[0].thefinal[0].id // 当我控制台 statusId 它显示得很好,但我需要一种方法来减少这个 static。 I've also made a map我还做了一个 map

   <div>  {subCategory.slice(0, max).map((item, index) => <div key={index}>{subCategory.item}</div>)}</div>

It successfully shows a list of ids, even though I won't need them in such format.它成功地显示了一个 id 列表,即使我不需要这种格式的它们。 So I can access the data, just not the right way.所以我可以访问数据,只是方式不对。

Do I need to do multiple maps, one after the other?我需要一张接一张地做多张地图吗?

PT.2 I eventually need to pass this data from different arrays into a carousel. PT.2 我最终需要将这些数据从不同的 arrays 传递到轮播中。 I also managed to print all the values for specific parameters but I would have to show one at a time.我还设法打印了特定参数的所有值,但我必须一次显示一个。 For example I'll have 5 slides;例如,我将有 5 张幻灯片; 5 carousel items that show only one id at a given slide (slide 1: id 1; slide 2: id:2) 5 个轮播项目,在给定幻灯片上仅显示一个 id(幻灯片 1:id 1;幻灯片 2:id:2)

I'm sorry if it doesn't make much sense, I could try to give more info or maybe even sanbox of some sort.很抱歉,如果它没有多大意义,我可以尝试提供更多信息,或者甚至是某种类型的 sanbox。 I've spent so much time trying to figure this out that I can't think clearly anymore and I have nobody to ask.我花了很多时间试图弄清楚这一点,以至于我无法再清楚地思考,也没有人可以问。 It may seem as an easy task but I'm just a beginner and it's overwhelming.这似乎是一项简单的任务,但我只是一个初学者,而且它是压倒性的。

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

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