Bert
Bert

Reputation: 350

Create unknow amount of objects in javascript

I have the following situation, what I want to know is how I go about creating the objects (since I have no idea how many there will be):

jQuery.each($itemList, function() {
    <something> = new ItemObject(this[1], this[2]);
});

Can something be an array? If so, do I use .push? or is there a better way of doing this?

Upvotes: 1

Views: 47

Answers (1)

jAndy
jAndy

Reputation: 236192

Its probably the most convinient way to push those newly created objects into an Array.

var objectList = [ ];

jQuery.each($itemList, function() {
   objectList.push( new ItemObject(this[1], this[2]) );
});

// somewhere else
objectList.forEach(function( obj ) {
    // do something great
});

Upvotes: 2

Related Questions