I’m building an array of objects which have a property of type array:
here’s some simplified code:
var _data = [];
for(var i=0;i<10;i++) {
var element = {
id: i,
answers: []
};
for(var j=0;j<3;j++) {
var answer = {
id: j,
description: ''
};
element.answers.push(answer);
}
_data.push(element);
}
At the end of the two cicle the array _data has 10 elements but each element has the property answer empty (I expect 3 items for each element).
Why this happens? It seems like the push doesn’t push the entire object but only the “first level properties”.
thanks
Running your code in Firefox 8 results in the following _data array:
_data:
Maybe you have a typo because it is
answersnotanswer