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

178
Visualizações
React web app: 'Hide' elements of a table so those with value of zero are not shown

I have created an educational app to list products for an online store. I have created a table for these products, but I would like to hide those whose quantity have run out (i.e., hide the whole row when quantity=0).

I create the table from arrays in the state using array.map:

<table className="center-table" style={{border:'solid',}}>
      <thead className="has-text-black-bis" style={{backgroundColor:"#e6be8a"}}>
        <tr>
          <th >Product Name:</th>
          <th >Unit Price:</th>
          <th >Quantity Available:</th>
          <th >Buy!</th>
        </tr>
      </thead>
      <tbody className="has-text-black-bis">
        {this.state.indices.map((a) => (
          <tr className="rows">
            <td ><strong>{this.state.itemNames[a]}</strong></td>
            <td ><strong>{this.state.costs[a]} Wei</strong></td>
            <td ><strong>{this.state.quantities[a]}</strong></td>
            <td >
              Qty: <input type="text" className='table-input' name="inputs" value={this.state.inputs[a]} onChange={this.handleInputChange} />
              <button type="button" className='buy-btn' onClick={()=>this.buyItem(a)}> Buy!</button>
            </td>
          </tr>
        ))}
      </tbody>
    </table> 

I would imagine a for loop would work well, but I have not been able to implement this within JSX.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Just add a filter before the map method:

this.state.indices.filter((a) => a.quantity !== 0).map((a) => (...

You can also just go

this.state.indices.filter((a) => a.quantity).map((a) => (...

Because 0 is considered falsy in JS.

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