简体   繁体   English

如何在 React js 中使用 map 函数获取嵌套循环值?

[英]How to get nested loop value using map function in React js?

How to get TC id value and using map function in react js如何在react js中获取TC id值并使用map函数

Here's my code json这是我的代码 json

[
    {
        "Results": {
            "TC": [
                {
                   "id": 1
                 },
                 {
                   "id": 2
                 },
                 {
                   "id": 3
                 }
            ]
        }
   }
]

Here's my React Code:这是我的反应代码:

import PostData from './result_log.json';

{PostData.map((PostDetail, index) => {
    return <h1>{PostDetail.Results.TC.length}</h1>
})}

Here's how to create a h1 balise tag for each id inside your TC array in your json code.以下是如何在 json 代码中为TC数组中的每个 id 创建h1 balise 标签。

import PostData from './result_log.json';

const getAllIds = () => {
  const idsArray = PostData.map(item => {
    return item.Results.TC.map(it => <h1>{it.id}</h1>)
  }.reduce((acc, curr) => [...acc, ...curr], [])

  return idsArray
}

getAllIds()

I have flattened the first map result so you have only one array containing every id key value of your json code.我已经展平了第一个地图结果,因此您只有一个包含 json 代码的每个 id 键值的数组。

https://jsfiddle.net/v5r8oyds/ https://jsfiddle.net/v5r8oyds/

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

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