James Delaney
James Delaney

Reputation: 1776

How to intizialize and push objcet in array, into constructor?

There is in application "target" drop-down, witn two elements in that drop-down. I created Model (NameValueModel) with two parametars and construcor:

1. name:string
2. value:string
3. constructor(name, value) { this.name=name; this.value=value; }

In main class I want to create:

1. list of this objects
2. initialize the list in constructor
3. After that push two new NameValueModel-s in that list. (This step is problem for me)

I need to push this objects: 
a) name: "New window", value: "_blank"
b) name: "Current window", value: "_self"

What I do so far is:

1. Create Model with 2 parmas and constrcutor

2. In main class create 
a) List of objects: selectedTargets: NameValueModel[]; 
b) Trying to push objects into lsit:
 constructor() {
   this.selectedTargets.push(new NameValueModel("Current window", "_self")); 
   this.selectedTargets.push(new NameValueModel("New window", "_blank"));
 }

On the end I need to provide: let target = this.selectedTargets.value;

But compailer does not give me to put Value, only "Values" this.selectedTargets.values;

How I can fix this? Or this way of implemetation is ok?

Upvotes: 0

Views: 43

Answers (1)

Abdelrahman Hussien
Abdelrahman Hussien

Reputation: 505

I can't understand you very well, but I guess this is what you need, you need to initiate the array first

 selectedTargets:NameValueModel[]=[]; 
    constructor() {
      this.selectedTargets.push({name:"Current window", value:"_self"}); 
      this.selectedTargets.push({name:"New window", value:"_blank"});
    }

Upvotes: 2

Related Questions