In the code below, I am picking through lines of an array, do some work on them, and push them (multiple times each line) into different arrays. While doing so, they seem to keep some kind of dependency to their template which I cannot find. If I change one item in the end-result-array
, every line, which had the same template changes as well. Before pushing the lines into the array I used .concat()
to create new objects and break the depency to the template, but it doesnt seem to work.
Most of the variables are named in german, but I tried to give some explanation in english comments.
If you run the code, the output will show 3
changed lines where I only changed one of them.
Can someone find the problem?
EDIT: I have switched the whole code. This one is a strapped down version of the old code, but much easier to read and understand. I have deleted everything irrelevant to the real problem. The problem stays the same: Changing one line in the "result" array, changes 3
lines at once, and I dont know why.
//data array
var ABF = [
[1,5,[0]],
[2,5,[0]],
[3,5,[0]],
[4,5,[0]]
];
//result array
var result = [];
//calling the function to start everything up
main(function(mainResult){
});
function main(cbmain){
//call intern function
intern (function (internResult){
//output to see results
console.log('Output before changing one line:');
for (var n=0; n<result.length; n++){
console.log(''+result[n][2]);
}
//changing one line only!
console.log('now change one line only!')
result[0][2].push(6);
//second output to see how many lines will change
console.log('Output after changing one line:');
for (var n=0; n<result.length; n++){
console.log(''+result[n][2]);
}
cbmain('ok');
});
//this function multiplies the array entries
function intern(cbintern){
for (var i=0; i<ABF.length; i++){ //for every entry of the data array
for (var e=0; e<3; e++){ //create 3 copies
var input = ABF[i].concat(); //with concat I am trying to copy the resultay, so there wont be any dependencies between the objects (which there still are, and I dont know why)
result.push(input); //pushing into this resultay for easier observation of the output
}
}
cbintern('done');
}
}