I'm unsure what the problem is here, but here is my code
var btotal = [];
var songarray = [];
function whileplaying() {
var playingSound = soundManager.getSoundById('aSound');
for (var i=0;i<8;i++) {
var ttotal = 0;
for (var n=0;n<32;n++) {
var eblock = (i*32)+n;
ttotal = ttotal+(playingSound.eqData.left[eblock]*100);
}
ttotal = ttotal/32;
btotal[i] = ttotal;
}
console.log(btotal);
songarray.push(btotal);
}
To explain, while the sound is playing I get an array with a length of 8, summing up the EQ values of that frequency block. At the end I append this array to songarray, so the theory is that songarray is an array containing the sound's EQ data in arrays.
The problem is that when I then get the value of songarray, it has however many arrays in it that I expected, but they are all identical, and are all what the last data point was, the latest value of btotal.
So this code would run, and console would display (for example):
[42.743750000000006, 2.98125, 0.10625000000000001, 0, 0, 0, 0, 0]
[38.859374999999986, 2.8, 0.09375, 0, 0, 0, 0, 0]
[56.26874999999998, 21.831250000000004, 3.853125, 0.340625, 0, 0, 0, 0]
[46.459374999999994, 19.584374999999998, 1.4, 0, 0, 0, 0, 0]
[38.08125, 11.8, 1.0750000000000002, 0, 0, 0, 0, 0]
However the contents of song array goes like:
0: Array[8]
0: 38.08125
1: 11.8
2: 1.0750000000000002
3: 0
4: 0
5: 0
6: 0
7: 0
length: 8
__proto__: Array[0]
1: Array[8]
0: 38.08125
1: 11.8
2: 1.0750000000000002
3: 0
4: 0
5: 0
6: 0
7: 0
length: 8
__proto__: Array[0]
Which are all the contents of the latest btotal array. This leads me to believe that each entry in songarray merely points to btotal. So how do I make it so that each array in songarray is the value of btotal at the time I append it, and not merely a pointer?
playingSoundvariable is the exact same in every iteration as you set it outside the loop, and never change it. Did you expect it to magically change?