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

118
Visualizações
What is the best way for assigning 2 different type object arrays to eachother in typescript?

I want to assign 2 different type array to eachother attribute by attribute. Can someone explain me what is the best way for this purpose? I am using the code below, but when I have too much attribute , it does not good look. How can I map attributes easily?

    export interface ClassA {
        Type: string;
        Name: string;
    }

    export interface ClassB {
        Typ: string; 
        Nm: string; 
    }



    let  items: ClassA[] = [
    { Type: 'T1', Name: 'N1'},
    { Type: 'T2', Name: 'N2'},
    { Type: 'T3', Name: 'N3'},
    ];

     
    let  items2:ClassB [] = [];


   items.forEach((item, index) => {
            let obj: ClassB  = {Typ:item.Type, Nm:item.Name };
            items2.push(obj);          
        });

console.log(items2);
almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use map instead since you can map from one type to another type with it. With explicit type annotations, it also gives you inference and typechecking in the map predicate as well:

let items2: ClassB[] = items.map((item) => ({ Typ: item.Type, Nm: item.Name }));

Playground

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