angular - How can I define an array with dynamic keys using Typescript? -


i'm using ionic 2, sits on top of angular 2. need create array of items. don't know how many items going in array.

here typescript, simplified:

import { component } '@angular/core';  import { vgapi } 'videogular2/core';  @component({   selector: 'page-class',   templateurl: 'class.html' }) export class classpage {   api: any[];    constructor(...){}    // load api when videos ready   onplayerready(api: vgapi, i: any) {     this.api[i] = api;   }  } 

onplayerready called when video players in view intialize. i id of player (0, 1, 2, ...).

i'd expect construct array of:

this.api[0] = vgapi (of player 1) this.api[1] = vgapi (of player 2) this.api[2] = vgapi (of player 3) this.api[3] = vgapi (of player 4) this.api[4] = vgapi (of player 5) 

unfortunately, following:

runtime error cannot set property '1' of undefined 

i believe because this.api[0] isn't explicitly defined anywhere. that's problem, don't know how many items be. how can define array allow behaviour?

it's because api undefined, have set empty array:

@component({   selector: 'page-class',   templateurl: 'class.html' }) export class classpage {   // initialize empty array   public api: vgapi[] = [];    constructor(...){}    // load api when videos ready   onplayerready(api: vgapi, i: number) {     this.api[i] = api;   } } 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -