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

188
Visualizações
How do I a space between elements of flatMap function?

I am putting together a Pokédex application for a school project. My tutor suggested I use a flatMap function to display pokémon types and abilities as there is sometimes more than one.

However, they just get listed with commas between them and no spacing. I'd like to add some spacing between the pokémon types. Can anyone help me with how to do that?

Code below for how I get the data:

function loadDetails(pokemon) {
    let url = pokemon.detailsUrl
    return fetch(url)
        .then(function (response) {
            return response.json()
        })
        .then(function (details) {
            pokemon.imageUrl = details.sprites.front_default
            pokemon.height = details.height
            pokemon.weight = details.weight
            pokemon.abilities = details.abilities.flatMap(
                (element) => element.ability.name
            )
            pokemon.types = details.types.flatMap(
                (element) => element.type.name
            )
        })
        .catch(function (e) {
            console.error(e)
        })
}

Code below for how it is displayed:

function showModal(pokemon) {
    let modalBody = $('.modal-body')
    let modalTitle = $('.modal-title')

    //empty the modal before we start
    modalTitle.empty()
    modalBody.empty()

    // create the elements we want in the modal
    let nameElement = $(
        '<h1 class="text-capitalize">' + pokemon.name + '</h1>'
    )
    let imageElement = $('<img class="modal-img">')
    imageElement.attr('src', pokemon.imageUrl)
    let heightElement = $(
        '<p>' + '<b>Height: </b>' + pokemon.height + '</p>'
    )
    let weightElement = $(
        '<p>' + '<b>Weight: </b>' + pokemon.weight + '</p>'
    )
    let typesElement = $(
        '<p class="text-capitalize">' +
            '<b>Types: </b>' +
            pokemon.types +
            '</p>'
    )
    let abilitiesElement = $(
        '<p class="text-capitalize">' +
            '<b>Abilities: </b>' +
            pokemon.abilities +
            '</p>'
    )

    // append the elements to the modal
    modalTitle.append(nameElement)
    modalBody.append(imageElement)
    modalBody.append(heightElement)
    modalBody.append(weightElement)
    modalBody.append(typesElement)
    modalBody.append(abilitiesElement)

    $('#detailsmodal').modal()
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I used Array.join to control how the information is displayed, like so:

let abilitiesElement = $(
            '<p class="text-capitalize">' +
                '<b>Abilities: </b>' +
                pokemon.abilities.join(', ') +
                '</p>'
        )
about 4 years ago · Juan Pablo Isaza Relatório

0

What you currently have as result (with commas) is the string representation of an array. You could use join() to join the array with a space.

const pokemons = ["Charmander", "Bulbasaur", "Squirtle"];

console.log(`${pokemons}`); // string representation of array (,)
console.log(pokemons.join(" ")); // spaces ( )

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