Home > Net >  Map through element in JSON and accessing and other element in React
Map through element in JSON and accessing and other element in React

Time:09-20

I need to loop through a certain element in a JSON and then accsess the same position of a other element in the same JSON.

  content: {
    body: [
      {
        tag: "question1",
        question: "Do you want ...?",
        nextTag: ["question2", "question3"],
        nextTagDesc: ["yes", "no"],
      },

This is my json. I want to map through the content.body[0].nextTag and print the content.body[0].nextTagDesc

Thanks for all help.

CodePudding user response:

Try this

{content.body[0].nextTag.map((_, index)=>{
  return {
    <Text>{content.body[0].nextTagDesc[index]}</Text>
  }
}}

CodePudding user response:

const employeeInfo = {
    content: {
        body: [
      {
        tag: "question1",
        question: "Do you want ...?",
        nextTag: ["question2", "question3"],
        nextTagDesc: ["yes", "no"],
      },
    ]
    }
}
console.log(employeeInfo.content.body[0].nextTagDesc)

      {employeeInfo.content.body[0].nextTagDesc.map((value, index)=>{
        return {
              <Text>
                 {employeeInfo.content.body[0].nextTagDesc[index]} 
               </Text>
             }
     }}
  • Related