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

72
Visualizações
Transform datastructure with TypeScript

I would like to normalize and transform my data with TypeScript. The data that I get looks like this:

packages = {
  ungroupped: [
    {
      id: '0',
      status: 'active',
      owner: 'Stan Smith'
    },
    {
      id: '1',
      status: 'active',
      owner: 'Jill Smith'
    }
  ],
  groupped: [
    {
      name: 'Group 1',
      id: 1,
      accounts: [
        {
          id: '3',
          status: 'inactive',
          owner: 'Jane Smith'
        },
        {
          id: '4',
          status: 'hidden',
          owner: 'Stan Smith'
        }]
    }
   ]
};

What I would like to achieve, is this structure:

packages = [
  {
   name: null,
   id: null,
   accounts: [
     {
      id: '0',
      status: 'active',
      owner: 'Stan Smith'
     },
     {
      id: '1',
      status: 'active',
      owner: 'Jill Smith'
     }]
  },
  {
   name: 'Group XZY',
   id: 1,
   accounts: [
     {
      id: '3',
      status: 'inactive',
      owner: 'Jane Smith'
     },
     {
      id: '4',
      status: 'hidden',
      owner: 'Stan Smith'
     }]
  }
];

This is the code that I used to transform my data, but the result is not exactly what I want to have:

interface AccountGroupsUI {
   id: string;
   name: string;
   accounts: AccountUI[];
}

groups: AccountGroupsUI;

ngOnChanges(changes: SimpleChanges) {
  this.groups = [...this.packages.groupped];
  this.packages.ungroupped.map(data => {
    let newPackage = {
      id: null,
      name: null,
      accounts: this.packages.ungroupped
    };
    this.packages.push(newPackage);
  }
}

Now the code creates 2 "ungroupped" objects with the same content. I would like just one object with id: null, name: null, and all ungroupped account pushed into the accounts, but I cannot figure out how to do that. Can someone please help me?

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

0

This looks like what you're after:

const packages = {
  ungroupped: [{
    id: '0',
    status: 'active',
    owner: 'Stan Smith'
  }, {
    id: '1',
    status: 'active',
    owner: 'Jill Smith'
  }],
  groupped: [{
    name: 'Group 1',
    id: 1,
    accounts: [{
      id: '3',
      status: 'inactive',
      owner: 'Jane Smith'
    }, {
      id: '4',
      status: 'hidden',
      owner: 'Stan Smith'
    }]
  }]
};

const result = [{
    id: null,
    name: null,
    accounts: packages.ungroupped
  },
  ...packages.groupped
];

console.log(result);


Note that groupped and ungroupped are conventionally spelled grouped and ungrouped.

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