0

How to get TC id value and using map function in react js

Here's my code 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>
})}
11
  • Do you to create a separate h1 tag for each id? Commented Jan 5, 2020 at 15:31
  • yes i need to create h1 tag for each id Commented Jan 5, 2020 at 16:04
  • @GurumoorthyDhanabal Does my answer fullfill your needs ? Commented Jan 5, 2020 at 16:43
  • @Hurobaki I was tired but it's not working for me. can you provide me any jsfiddle link for this example Commented Jan 6, 2020 at 1:24
  • I've provided you a jsfiddle link inside my answer Commented Jan 6, 2020 at 10:39

1 Answer 1

1

Here's how to create a h1 balise tag for each id inside your TC array in your json code.

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.

https://jsfiddle.net/v5r8oyds/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.