Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

70
Views
react I want to output the array in multiple tags rather than one tag

We have now put json array data in the functionSupplement variable. If you print this <p>functionSupplement </p>, you will get something like this.

How can I write code to get it to work the way I want it to?

<p>
"Hi" 
"Hi"
</p>

I want to make it like this.

<p>
"Hi"
</p>
<p>
"Hi"
</p>

This is my code.

let functionSupplement = ProductDetail && ProductDetail.chart?.functionalsInfo?.[0].materialsInfo.map(array => array.ingredientsInfo?.[0].mFunctionalDisplayText);

console.log(functionSupplement) // (2) ["Hi","Hi]
   
return (
          <div>
          <p>{functionSupplement}</p>  //<p>"Hi" "Hi"</p>
          </div>
)
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use the .map property like so :

let functionSupplement = ProductDetail && ProductDetail.chart?.functionalsInfo?.[0].materialsInfo.map(array => array.ingredientsInfo?.[0].mFunctionalDisplayText);

console.log(functionSupplement) // (2) ["Hi","Hi]

    return (
        <div>
          {functionSupplement.map(item=> <p>{item}</p>)}
        </div>
    )
}
about 4 years ago · Juan Pablo Isaza Report

0

You'll want to use map on the list.

For more info, see: https://reactjs.org/docs/lists-and-keys.html

Here's a working example based on your code:

<!DOCTYPE html>
<html>

  <body>

    <div id="root"></div>
    <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

<script type="text/babel">
    'use strict';

    let functionSupplement = ["hi", "hello"];
    
    const paragraphs = functionSupplement.map((item) =>
      <p key={item}>{item}</p>
    );

    ReactDOM.render(paragraphs,document.getElementById('root'));
</script>

  </body>
</html>

about 4 years ago · Juan Pablo Isaza Report

0

String with newlines /n

I would assume you just want your content to appear on separate lines.

You can use the <pre> tag to render content as it is.

const content = `this is line 1
this is line 2
this is line 3`


class Snippet extends React.Component {
   render() {
      return (<div><pre>{ content }</pre></div>)
   }
}


ReactDOM.render(
  <Snippet />,
  document.body
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Arrays

If you are rendering an array you can also use .map to loop through the array.

const content = ['this is line 1','this is line 2','this is line 3']


class Snippet extends React.Component {
   render() {
      return (<div>{ content.map( (x,i) => <p key={i}>{x}</p>) }</div>)
   }
}


ReactDOM.render(
  <Snippet />,
  document.body
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!