Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

77
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda