istrau2
istrau2

Reputation: 367

add object to javascript array where the list specifies the type

In my program I have an array of javascript objects (my angular model. I am obtaining it from a web service call). I would like to add a new object to the array and I would like the new object to be of the same type as the others. Is there any way to do this other than writing a class and instantiating an object of that type. I mean, is there any way to say "add another member to this list, make it like all the others"?

Thanks in advance

Upvotes: 0

Views: 55

Answers (1)

Eyal Ofri
Eyal Ofri

Reputation: 700

If you created a constructor function like:

function Dog(){
this.age = 13;
}

It will take only 1 line to add a new Dog to an array:
array.push(new Dog());

Edit:
If you want to get a new object and the type defined in the web service you should first create a constructor function as I wrote before. You can't get the structure in any other (efficient) way

Upvotes: 1

Related Questions