javascript array/object issue
- by FlyingCat
I am trying to push elements to an array.
for (a=0; a < test.length; a++){
if(this.testnumber != test[a].number){
//it will only loop 8 times under conditional statement
group = {
title: test.Title ,
ID: test.ID,
contents: []
};
this.company.push(group);
this.testnumber = test.number[a];
}
//outside of if conditional statement.. it will loop 15 times
//i want every test[a].conetents get pushed to group.contents array.
//this.company is the final variable I need for this function...
group.contents.push(test[a].contents);
}
However, when I do
console.log(this.company);
I see 8 elmements with only 1 element in each group.contents array
The ideal situation is to have 8 elements with 2 to 3 elements in the group.contents array
I hope I explain my situation well. Any idea how to solve my issue? Thanks a lot!