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}>();
3
  • 1
    Hi Aleksander, It helps if you can include your code in the question as text, as it makes it easier to copy and revise without re-typing Commented Jun 14, 2016 at 22:28
  • 1
    There is an option to edit the question. As this is code, you can aslo format the code - select it and click on the {} icon Commented Jun 14, 2016 at 22:35
  • Yea I see now thanks Commented Jun 14, 2016 at 22:38

1 Answer 1

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"); 
)
Sign up to request clarification or add additional context in comments.

1 Comment

Please include in your constructor the initialization: this.name = _name; this.bio = _bio; this.job = _job; this.salary = _salary; this.url = _url;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.