0

I want to display an array as a string which is seperated by commas in react native. This is my code.

let currentWorkout = this.props.currentWorkout;
// tools is an array. want to display it as valueOne, valueTwo, valueThree etc.
    let tools = JSON.stringify(currentWorkout.tools);

    return (
      <View style={styles.container}>
        <WorkoutDetail
          workout={this.props.currentWorkout}
          workoutImage={currentWorkout.workoutImage}
          onPressWorkout={() => alert("CONTINUE WORKOUT")}
        />
        <View style={styles.workoutInfo}>
          <KeyValueText header="Tools" value={tools} /> ////
        </View>
      </View>

How can I achieve this?

1 Answer 1

1

You could do

let tools = currentWorkout.tools.join(', ');

instead of JSON.stringify and I think you'll got the results you want.

Check the documentation for the join method.

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

4 Comments

Thanks Khriz. But when I used this, it says, currentWorkout is undefined.
Well then currentWorkout.tools is not an array, you should check this.props.currentWorkout because you are passing no props to the component
It worked after this Khriz. let tools = currentWorkout.tools ? currentWorkout.tools.join(", ") : ""; Thank you very much.
Thank you very much

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.