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

114
Views
Inicializar matriz de tipo de interfaz en angular 11

Creé una interfaz con el nombre Prueba en Angular 11

 export interface Test { id:number; name:string; }

Luego exporté la interfaz y creé su matriz con el nombre de ProvinceAllStudent

 import { Test } from './../../../student/Models/test'; import { Component, OnInit } from '@angular/core'; import { Province } from 'src/app/student/Models/Province.model'; @Component({ selector: 'app-general-statistic', templateUrl: './general-statistic.component.html', styleUrls: ['./general-statistic.component.css'] }) export class GeneralStatisticComponent implements OnInit { Provinces:any[]; ProvinceAllStudent:Test[]=[]; constructor( ) { } ngOnInit(): void { this.CalculateProvinceStudents() } CalculateProvinceStudents() { for(let j=0;j<5;j++) { this.ProvinceAllStudent[j].id=j; this.ProvinceAllStudent[j].name='A'; } } }

cuando ejecuto la aplicación me sale el error

core.js:6210 ERROR TypeError: no se pueden establecer propiedades de undefined (estableciendo 'id') en GeneralStatisticComponent.CalculateProvinceStudents (general-statistic.component.ts:23)

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

primero debe crear el objeto antes de modificarlo en la matriz

 this.ProvinceAllStudent.push({ id: j, name: 'A' })
over 4 years ago · Santiago Trujillo Report

0

Porque this.ProvinceAllStudent[j] no está undefined e intenta asignarle un valor a una propiedad. ( undefined no tiene las propiedades id y name , por lo que se genera una excepción).

Sugiero usar el método push de la matriz. Por ejemplo

 for (let j = 0; j < 5; j++) { this.ProvinceAllStudent.push({ id: j, name: 'A' }); }

O como alternativa, puede agregar elementos por índice de esta manera:

 for(let j = 0; j < 5; j++) { this.ProvinceAllStudent[j] = {id: j, name: 'A'}; }
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!