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

146
Views
Reestructuración de matriz mecanografiada

Quiero reestructurar mi matriz, que obtuve de php, para que pueda usarse con mi ngFor.

Actualmente, mi matriz contiene ~2700 elementos que contienen un objeto con los siguientes datos:

 name: "Someone" shift: 3 week: 1 department_name: "somedepartment"

Quiero cambiarlo a matriz->semana->turno->departamento.nombre

En JS, esto sería fácil, pero en Typescript no puedo hacerlo funcionar, mi código real se ve así:

 test(data): void{ //data contains the actual array let user = []; for (let entry of data){ console.log(entry['week']); console.log(entry['shift']); console.log(entry['department_name']); user[entry['week']][entry['shift']][entry['department_name']] = entry; } console.log(user);}

console.log devuelve los primeros 3 valores: 1, 3, "departmentx"

Y luego regresa:

ERROR Error: no capturado (en promesa): TypeError: no se puede leer la propiedad '3' de undefined TypeError: no se puede leer la propiedad '3' de undefined

He leído que tengo que establecer el tamaño de la matriz antes, pero cada vez será diferente y luego habrá mucha más información además del nombre que se almacena.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Si entendí bien, desea cambiar el orden de los elementos dentro de su matriz, ¿verdad?

Esto debería funcionar:

 let newArray = []; for (let entry in data) { let currentObj = { [entry.week]: { [entry.shift]: entry.department_name } }; newArray.push(currentObj); } console.log(JSON.stringify(newArray));

la salida de esto sería algo como:

 newArray = [ 1: { 3: "somedepartmentX" }, 2: { 4: "somedepartmentY", }, 3: { 5: "somedepartmentZ", }, ];
over 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!