4

I am extracting {item.dosage} from a JSON which gives me the following string

"300gr short pasta, 2 spring onions, 50gr parmesan cheese"

and I'd like to show it each ingredient separated by comma in a separate text line like this

   300gr short pasta
   2 spring onions
   50gr parmesan cheese

I was trying to do the following within my JSX

{item.dosage.map((step, i) => (
        <Text>
            {i > 0 && ", "}
               {step}
        </Text>
 ))}

and I'm getting an error about "undefined is not a function...".

1
  • what is dosage? what is it contains? Commented Sep 16, 2020 at 12:14

2 Answers 2

4
const str = "300gr short pasta, 2 spring onions, 50gr parmesan cheese"

...
<Text>
  {str.split(',').map((step)=> <Text>{step}{",\n"}</Text>)}
</Text>
...

// so try this way
<Text>
 {item.dosage.split(',').map((step)=> <Text>{step}{",\n"}</Text>)}
</Text>
Sign up to request clarification or add additional context in comments.

1 Comment

it worked although I needed to adjust slightly the code with split(", ") and {"\n"}. Many thanks
2

in the first sight, it looks like that is getting close to what u look for this way u can give margins to the View as desired..

{item.dosage.split(', ').map((step, i) => (
  <View>
      <Text>
        {i > 0 && ", "}

         {step}
      </Text>
  </View>
 ))}

3 Comments

your solution does not seem to be working, it is still showing as a full string without comma separation.
what does item.dosage contains.? i have edited solution
it is commented in the first line of this post "300gr short pasta, 2 spring onions, 50gr parmesan cheese"

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.