Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda