Currently, I am developing an angular application that involves creating a custom array of services called Workbooks, each containing an array of services known as Views. However, while populating the array using a simple for loop, I encountered some unexpected outcomes:
Following the initial iteration, there is one workbook present in the array:
Workbook 1
Upon completing the second iteration, two workbooks appear titled "workbook 2": Workbook 2
Workbook 2
With the third iteration: Workbook 3
Workbook 3
Workbook 3
This pattern continues with subsequent iterations. I have included a simplified version of the code snippet below showcasing how the workbooks are created and added to the array:
for (var i = 0; i < 3; i++) {
var workbook = Workbook;
workbook.setTitle("workbook " + (i + 1));
for (var j = 0; j <2; j++ ) {
var view = View;
view.setTitle("view " + (j + 1));
workbook.addView(view);
}
workbooks[i] = workbook;
//this following loop can be utilized to print the array contents as mentioned
for (var k = 0; k < workbooks.length; k++) {
console.log(workbooks[k].getTitle());
}
}
return workbooks;
The issue arises when the ith workbook is allocated a specific title and placed into the respective spot within the array. How could it happen that when i
equals 2, a workbook labeled Workbook 3
gets assigned to indices 0, 1, and 2 simultaneously?
For further information, here is a demonstration on Plunker illustrating the relevant code section from the project. Any input or guidance would be greatly appreciated!