Is it possible to Unshift array?

Hello all,

I am new to cables, been struggling to find a method to add numbers at the beginning of an array. Any advice? Thanks!

Hi,

Here is an example how to add a Number in front of an existing Array.

With an Array Merge you can do the trick. By changing the number of the first Array you only change this part and not the original Array you use.
If you need multiple Numbers you can also use the ArrayBuffer and feed the Numbers into it. Each trigger adds a number.

I hope that was what you are looking for. If you need quick help you can ping me on discord.

Cheers,
dino

Thanks Dino. That cleared a couple of other concepts :+1:.

How do we address this scenario where there is some streaming data (series of numbers) and I need to append the data continuously at the beginning of the array. Hope I made sense.

Thanks!

Hi,

In the case that you have an exact amount of data points you want to add in front of the array you can use arraybuffer (https://cables.gl/op/Ops.Array.ArrayBuffer).
When initialised it contains Zeros and is continuously filled up. The problem is that you can’t change the array length dynamically as it resets the the array to Zeros.

There is also an append array (https://cables.gl/op/Ops.Array.ArrayAppendArray) maybe there is a simple way to rewrite the op to put the current array to the front.

Cheers,
dino

Yeah, the array buffer wasn’t helping this use case, am trying to start with an empty array and add continuously at the beginning, inverse of the append array like you mentioned. I’ll look into rewriting the OP.

Thanks!
Don.

You can just switch the order of the arrays in line 23 and it works.
What you want is arr=newArray.concat(arr);

image

Cheers,
dino

1 Like

That worked like a charm! :slight_smile:

Cheers,
Don