Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to apply conditionals in a nested map function?

I want to handle each string of my item.fieldtype result, but i’m not finding a way to do it someone knows how?

....
      <SafeAreaView style={{flex: 1}}>
          <View>
            {Object.keys(Body).length > 0 ? (
              Body.map(item => <Text key={uuid.v4()}>{item.field}</Text>
              // if (item.fieldtype == "Numeric") <Text>Numeric</text> ===> apply here
              //else if (item.fieldtype == "Text" ) <Text>Text</text> ===> "" ""      
              )
            ) : (
              <Text>Testado</Text>
            )}
          </View>
        </SafeAreaView>
...

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use a fragment tag (or just a View) and return multiple items in the map:

<SafeAreaView style={{flex: 1}}>
  <View>
    {Object.keys(Body).length > 0 ? 
      (Body.map(item => 
       <React.Fragment key={uuid.v4()}>
          <Text>{item.field}</Text>
          {item.fieldtype == "Numeric" ? <Text>Numeric</Text> : null}
          {item.fieldtype == "Text" ? <Text>Text</Text> : null}
       </React.Fragment>))
      : (<Text>Testado</Text>)}
  </View>
</SafeAreaView>

(Note that it would be ideal if you had useable stable ids rather than generating a new uuid for each item on each render)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading