Help - push an array into array simple question

Nevertheless, posting your code may help us understand what you are trying to do.

The example below initializes sampleBuffer with 32 inner Arrays of 3 random numbers each within the setup() function. The inner Arrays are not replicated.

Within draw(), it pushes an Array of 3 new random numbers onto the top of sampleBuffer and removes the oldest Array of 3 numbers from the beginning of sampleBuffer during each frame. It also calls a processTheData function during each frame. As written here, that function displays the contents of sampleBuffer. Run the code, and notice the changes in the data during each frame. You can call frameRate to slow it down temporarily, if necessary.

let sampleBuffer=[];
let bandsEnergy=[];

function setup() {
  createCanvas(400, 660);
  // initialize sampleBuffer
  for (let i=0; i<32; i++) {
    sampleBuffer.push([random(10), random(10), random(10)]);
  }
}

function draw() {
  background(239);
  processTheData(sampleBuffer);
  // push new data on sampleBuffer
  sampleBuffer.push([random(10), random(10), random(10)]);
  // remove data at index 0 of sampleBuffer
  sampleBuffer.shift();
 }
 
 function processTheData(data) {
  for (let i=0; i<data.length; i++) {
    text(data[i], 10, i * 20 + 20);
  }
 }

You can rewrite the processData function to do this.

That is because you populated sampleBuffer with 32 references to bandsEnergy.

1 Like