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

158
Visualizações
Trailing and leading commas added issue

I am calling a function from jsx as xyz(id) inside the <img src = "xyz(id)" />. In the calling function, I am retrieving the image value through a map as

const xyz = (id) =>{
     return abc.map(a => {
         if(a.id === id){
             if(a.url){
                 return a.url;
                 }
             }
         })
     }

When I inspect the img element trailing and leading commas are being appended after the return statement as "..returned_url..".

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

0

You need to only return the url part to the caller if there's a match - you need to exclude the empty results from the .map callback, else they'll appear as empty strings between lots of commas.

const xyz = (id) => {
    return abc
        .map(a => {
            if (a.id === id && a.url) {
                return a.url;
            }
        })
        .filter(Boolean);
}

But returning an array when you're trying to set a src doesn't make much sense. If there's only one possible matching item, using .find might be more appropriate than .map, perhaps

const xyz = (id) => {
    return abc
        .find(a => a.id === id)
        ?.url;
}
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