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

226
Visualizações
How to render propeties with nonzero values inside template strings

I am actually trying to render an HTMLElement using template strings. The function goes like this

function renderContent(){
  return `<div class="card">
              <span>Content goes here</span>
              <ul class="list">
                 ${renderList()}
              </ul>
          </div>`
}

And the renderList() function goes like this

function renderList() {
  const valueWithCount = { a : 1, b: 0, c:0, d:1}
  return Object.entries(valueWithCount).map(([key, value]) => {
    return `<li>
                    <span class="key">${key}:</span>
                    <span class="value">${value}</span>
            </li>`;
  });
}

I want the content to be rendered as

Content goes here
a:1
d:1

I dont want to render the the properties with value 0.I could write value && (some HTML content). But since its inside the template string, even 0 gets printed. If I go with ternary value ? some content : null , the behavior is same.The same case with undefined as well.

How do I avoid this?

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

0

Use .filter() instead of .map():

function renderList() {
  const valueWithCount = { a : 1, b: 0, c:0, d:1}
  return Object.entries(valueWithCount).filter(([key, value]) => {
    if(value !== 0)
    return `<li>
                    <span class="key">${key}:</span>
                    <span class="value">${value}</span>
            </li>`;
   return null;
  });
}

return null in other cases.

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