在Javascript中将数组复制到较大数组的中间

时间:2021-11-10 05:33:09

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.

所以我有2个Uint8类型的数组。

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:

我想用arr1从第4位开始替换arr2的内容。所以arr2将是:

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:

如果我没有尝试在数组的中间执行此操作,我可以像这样使用set:

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).

我知道我可以循环遍历arr2并单独复制值,但性能明智这与set相比非常慢(并且性能对我很重要,因为它每秒24次复制整个canvas画布img数据)。

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

是否有任何函数可以复制到数组的中间,但具有set的性能?

3 个解决方案

#1


4  

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

使用typedarray.set(array [,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).

到目标数组的偏移量,开始从源数组中写入值。如果省略此值,则假定为0(即,源数组将覆盖从索引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);

#2


2  

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).

.slice不会改变数组并返回它的新浅表副本(与splice不同)。

#3


1  

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

由于您使用的是类型化数组。你不能使用set方法的偏移量吗?

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

要从目标数组的第4个元素覆盖。 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.

对我来说,如果我理解你的问题,它就能满足您的需求。

#1


4  

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

使用typedarray.set(array [,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).

到目标数组的偏移量,开始从源数组中写入值。如果省略此值,则假定为0(即,源数组将覆盖从索引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);

#2


2  

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).

.slice不会改变数组并返回它的新浅表副本(与splice不同)。

#3


1  

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

由于您使用的是类型化数组。你不能使用set方法的偏移量吗?

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

要从目标数组的第4个元素覆盖。 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.

对我来说,如果我理解你的问题,它就能满足您的需求。