Aleksandar Milicevic
Aleksandar Milicevic

Reputation: 523

How to add values to array- Angular2

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}>();

Upvotes: 10

Views: 57280

Answers (1)

Andrei Zhytkevich
Andrei Zhytkevich

Reputation: 8099

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"); 
)

Upvotes: 21

Related Questions