10

I made an array like this in angular2, and also I made a form to fill out a table and it works well. But my question is what is the best way to put some values in this array without filling the form, I want some content already in this array.

employeeList = new Array<{name:string, bio:string, job:string, salery:string, url:string}>();
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567

1 Answers1

21

Create a class representing your structure:

export class User {
  name: string;
  bio: string;
  job: string;
  salary: string;
  url: string
  constructor(_name: string, _bio: string, _job: string, _salary: string, _url: string) {
    this.name = _name; this.bio = _bio; this.job = _job; this.salary = _salary; this.url = _url;
  }
}

or like this:

export class User {
  constructor(public name: string,
    public bio: string,
    public job: string,
    public salary: string,
    public url: string) {
  }
}

You array will look like this:

users: User[] = []; // if it's a class member
var users: User[] = []; // if it's a local variable

Add something to array:

this.users.push(
   new User("Bob", "", "Developer", "100", "github.com"); 
)
Andrei Zhytkevich
  • 8,039
  • 2
  • 31
  • 45
  • Please include in your constructor the initialization: this.name = _name; this.bio = _bio; this.job = _job; this.salary = _salary; this.url = _url; – Diego Sarmiento Jun 02 '17 at 01:16