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

192
Visualizações
Using .map() method to render array items into their own separate divs, but comma is still displaying?

I've searched and searched but I've only found solutions to .join() the items into a single string..

const createCard = () => {
    const pokemonTypes = ['grass', 'fire', 'water'];
      return (
        `<div>
          ${pokemonTypes.map(type => `<div>${type}</div>`)}
        </div>`
      )
}

document.body.insertAdjacentHTML("beforeend", createCard());

For some context, I am using an API but I simplified the code so it's quick and easy to read.

I am trying to display each string into its own div so that I can style each div separately...like color coded buttons: green for grass, blue for water, red for fire etc.

When I run the code the strings successfully display in their own div, however the comma remains. I'd like them to just display next to each without the comma separating them.

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

0

The .map() method returns an array. In the template literal `${}` js will convert this array to a string. It does that by default by joining the elements with comma's. You can join the array to a string yourself without a comma:

${pokemonTypes.map(type => `<div>${type}</div>`).join('')}
about 4 years ago · Juan Pablo Isaza Relatório

0

Array.map returns an array as the output. Setting this array as the html content is same as casting an array to a string.

Example

const numArray = [1, 2, 3];
console.log(numArray.toString());

Casting an array to a string will always includes the comma in between.

Your expression is same as below one

document.body.insertAdjacentHTML("beforeend", [1, 2, 3]);

What you have to do is you have to join this array with empty string and set this single string as the html contant as below.

Use .join('') to join an array without comma

const createCard = () => {
  const pokemonTypes = ['grass', 'fire', 'water'];
  return (
    `<div>
      ${pokemonTypes.map(type => `<div>${type}</div>`).join('')}
    </div>`
  );
}

document.body.insertAdjacentHTML("beforeend", createCard());

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use .join('') for string concrete and the same type used for styling.

const createCard = () => {
  const pokemonTypes = ['grass', 'fire', 'water'];
  return (
    `<div>
      ${pokemonTypes.map(type => `<div class="${type}">${type}</div>`).join('')}
    </div>`
  );
}

document.body.insertAdjacentHTML("beforeend", createCard());
.grass {
  color: green
}

.fire {
  color: red
}

.water {
  color: blue
}

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