PDA

View Full Version : object + array + object



occiso
19 Oct 2010, 2:11 PM
Regards,

I created an object containing an array containing an object:



var obj =
{
param: [
{idealp:"aa",ideala:"aa", ideApp:"aa",tipApp:"aa"},
{idealp:"aa",ideala:"aa", ideApp:"aa",tipApp:"aa"},
{idealp:"aa",ideala:"aa", ideApp:"aa",tipApp:"aa"},
{idealp:"aa",ideala:"aa", ideApp:"aa",tipApp:"aa"},
{idealp:"aa",ideala:"aa", ideApp:"aa",tipApp:"aa"}
]
}The problem is that I want the array is dynamic and can add rows as needed in a loop. In the example I am limited by five parameters and would like to create n.



for (var index = 0; index < 10; index++) { //insufficient array error (is undefined)
obj.param[index].ideala= index,
obj.param[index].idealp= index+1
}

any ideas ?

Keylan
19 Oct 2010, 2:21 PM
What exactly are you trying to do in the loop? The error is caused by trying to access an index of the array that does not exist.

plalx
19 Oct 2010, 5:17 PM
I don't get what you are trying to do...?

Condor
19 Oct 2010, 10:48 PM
So you want:

for (var index = 0; index < obj.param.length; index++) {
obj.param[index].ideala = index;
obj.param[index].idealp = index + 1;
}
(although I have no idea why)

occiso
19 Oct 2010, 11:18 PM
sorry, I do not explained well.

I have a store and is the store that fill in the array. But the store may have 5 or 20 records, so the array that I should have the same size as the store. the idea is:



for (var index = 0; index <store.length; index++) { //insufficient array error (is undefined)
obj.param[index].ideala= index,
obj.param[index].idealp= index+1
}

Condor
19 Oct 2010, 11:22 PM
You'll have to explain better.

You have some data (obj) and you have a store. What do these two have to do with each other?

Animal
20 Oct 2010, 12:04 AM
Regards,

I created an object containing an array containing an object:



var obj =
{
param: [
{idealp:"aa",ideala:"aa", ideApp:"aa",tipApp:"aa"},
{idealp:"aa",ideala:"aa", ideApp:"aa",tipApp:"aa"},
{idealp:"aa",ideala:"aa", ideApp:"aa",tipApp:"aa"},
{idealp:"aa",ideala:"aa", ideApp:"aa",tipApp:"aa"},
{idealp:"aa",ideala:"aa", ideApp:"aa",tipApp:"aa"}
]
}The problem is that I want the array is dynamic and can add rows as needed in a loop. In the example I am limited by five parameters and would like to create n.



for (var index = 0; index < 10; index++) { //insufficient array error (is undefined)
obj.param[index].ideala= index,
obj.param[index].idealp= index+1
}

any ideas ?

obj.param.push(// a new object);