Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy an array into the middle of a larger array in Javascript

I've searched through the answers here, but I can only find this question answered for other languages.

So I have 2 Uint8 typed arrays.

var arr1 = [0,0,0];
var arr2 = [0,1,2,3,4,5,6,7,8,9];

I want to replace the contents of arr2 with arr1 starting at the 4th position. So that arr2 will be:

arr2 = [0,1,2,0,0,0,6,7,8,9];

If I wasn't trying to do this in the middle of the array I could use set like this:

arr2.set(arr1);

And I would get:

arr2 = [0,0,0,4,5,6,7,8,9];

I know I can loop through the arr2 and individually copy the values, but performance wise this is very slow compared to set (and performance matters to me because it's copying an entire array of canvas img data 24 times a second).

Is there any function that can copy into the middle of an array, but with the performance of set?

like image 834
YAHsaves Avatar asked Dec 31 '17 23:12

YAHsaves


People also ask

How do I copy part of an array to another array?

The Array. Copy() method in C# is used to copy section of one array to another array. Array. Copy(src, dest, length);

How do I copy an array part in JavaScript?

How to copy Array elements in JavaScript using concat() method. “concat()” is another useful JavaScript method that can assist you in copying array elements. In the concat() method, you can take an empty array and copy the original array elements to it. It will create a fresh copy of the specified array.

How do you push an array into an array?

The arr. push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array. Parameters This method contains as many numbers of parameters as the number of elements to be inserted into the array.

How do you push to the middle of an array?

Adding Elements to the Middle The splice() function also lets you add elements to the middle of the array. JavaScript arrays have a push() function that lets you add elements to the end of the array, and an unshift() function that lets you add elements to the beginning of the array.


3 Answers

Use the typedarray.set(array[, offset]) offset.

offset Optional

The offset into the target array at which to begin writing values from the source array. If you omit this value, 0 is assumed (that is, the source array will overwrite values in the target array starting at index 0).

const arr1 = new Uint8Array([0,0,0]);
const arr2 = new Uint8Array([0,1,2,3,4,5,6,7,8,9]);

arr2.set(arr1, 4);

console.log(arr2);
like image 61
Ori Drori Avatar answered Oct 19 '22 13:10

Ori Drori


You can use the slice method with the spread syntax:

const shim = (source, index, target) => [
  ...source.slice(0, index),
  ...target,
  ...source.slice(index)
]

var arr1 = [0,0,0];
var arr2 = [0,1,2,3,4,5,6,7,8,9];

const newArr = shim(arr2, 3, arr1);
console.log(newArr);

.slice will not mutate the array and will return a new shallow copy of it (unlike splice).

like image 43
Sagiv b.g Avatar answered Oct 19 '22 14:10

Sagiv b.g


Since you are using typed array. Don't you can use the offset of the set method?

arr2.set(arr1, 3)

To overwrite from the 4th element of the target array. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set

To me it does just what you need, if I understand your question.

like image 1
Eineki Avatar answered Oct 19 '22 14:10

Eineki