Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
¿Cuál es la mejor manera de asignar 2 matrices de objetos de tipo diferente entre sí en typescript?

Quiero asignar 2 matrices de tipos diferentes entre sí, atributo por atributo. ¿Alguien puede explicarme cuál es la mejor manera para este propósito? Estoy usando el siguiente código, pero cuando tengo demasiado atributo, no se ve bien. ¿Cómo puedo mapear atributos fácilmente?

 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 answers
Answer question

0

Use map en su lugar, ya que puede mapear de un tipo a otro con él. Con anotaciones de tipo explícitas, también le brinda inferencia y verificación de tipo en el predicado del mapa:

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

Patio de juegos

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!