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

166
Visualizações
How to create a comma seperated list from multiple items in angular?

At my angular application, I'm trying to create a comma separated list of values I've got from my API response.

The API response looks like this:

    {
       "release_date":"2012-03-14",
       "some_relation":[
          {
             "id":"2604ebbf-4eb5-46e3-89d8-ab4e8ecc8275",
             "name":"ABC"
          },
          {
             "id":"5267a0c6-9423-4e28-a413-133cc3435612",
             "name":"DEF"
          },
          {
             "id":"13d1454a-fc0e-457c-9f8e-9a9952984d8c",
             "name":"GHI"
          }
       ],
    }

some_relation in nested, so I created an interface for this logic to work:

    export interface SomeResponse {
        some_relation: some[];
    }

    export interface some {
      id: string;
      name: string;
    }

At my html template, I now do this:

      <div *ngFor="let some of some_response.some_relation">
        <small>{{ some.name }}</small>
      </div>

Which returns the following output:

ABC

DEF

GHI

Instead of multiple lines outputted, I would like to have it that way:

ABC, DEF, GHI

How can this be accomplished?

Thanks in advance.

almost 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can do it like this:

  • Change div for span so it would not put each item in new line
  • Add , only if it's not the last item
<span *ngFor="let item of some_response.some_relation; let last = last">
  <small>{{ item.name }}</small><span *ngIf="!last">, </span>
</span>
almost 4 years ago · Santiago Trujillo Relatório

0

That's because you are using *ngFor on division tag instead of small tag and it is creating 3 division with a small tag in each division. Division is a block element and each division is rendered on the next line. Try doing something like this for single line rendering:

<small *ngFor="let some of some_response.some_relation" >
{{ some.name }} 
</small>

Edit: Since you want the comma in between you can use this:

<small> 
{{ some_response.some_relation.reduce((total, obj, index) => index? total + ", " + obj.name: obj.name, "") }} 
</small>
almost 4 years ago · Santiago Trujillo 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