I have a place in my javascript code where I need to do an operation like
copying a specified range from one array into specified range in another array.
The operation is similar to
1) System.arraycopy in java ->
System.arraycopy(array1, index1, array2, index3, index4 - index4 + 1);
2) copy in go ->
copy(array2[index3:index4 + 1], array1[index1:index2+1])
3) slice in python ->
array2[index3: index4 + 1] = arr[index1: index2+1]
For now I am iterating it by hand and doing it. But I did not any util function in js to do that. Is there really one ?
Update 1: It should exactly only copy and not add or remove elements from any of the two given arrays.
It should behave like this implementation:
function arraycopy(src, srcPos, dst, dstPos, length) {
let j = dstPos;
let tempArr = src.slice(srcPos, srcPos + length);
for (let e in tempArr) {
dst[j] = tempArr[e];
j++;
}
};
Update 2: (See some answers below with caution and see if it will fit your use case(like in huge dataset case)) Many answers below used splice in such a way it will break, if the range start to end in source array is huge. It will throw "RangeError: Maximum call stack size exceeded"... as it will exceed the maximum allowed number of arguments in a function. (try demo script here) "
Length of the array to get copied. This method copies the specified range of the specified array into a new array. The array can be copied by iterating over an array, and one by one assigning elements. clone () creates a new array of the same size, but System.arraycopy () can be used to copy from a source range to a destination range.
Method 1: Iterating each element of the given original array and copy one element at a time. With the usage of this method, it guarantees that any modifications to b, will not alter the original array a, as shown in below example as follows: In the previous method we had to iterate over the entire array to make a copy, can we do better?
System.arraycopy () is faster than clone () as it uses Java Native Interface (Source : StackOverflow) If you want to copy the first few elements of an array or a full copy of an array, you can use Arrays.copyOf () method. Arrays.copyOfRange () is used to copy a specified range of an array.
Then put each source array stacks of paper through a copy-machine and staple the resulting two copies together. Go to the store, buy enough paper for a single copy of the first source array. Then, copy the source array to the new paper by hand, ensuring to fill in any blank sparse spots.
A combination of slice
and splice
is probably the way to go here. You can write a helper function that mimics the same behavior in other languages. Here's one that mirrors Java's arraycopy
method:
const arr1 = [1, 2, 3, 4, 5, 6];
const arr2 = ["a", "b", "c", "d", "e", "f"];
function arrayCopy(src, srcIndex, dest, destIndex, length) {
dest.splice(destIndex, length, ...src.slice(srcIndex, srcIndex + length));
}
// copy from arr1, at position 0, into arr2, at position 2, 3 elements.
arrayCopy(arr1, 0, arr2, 2, 3);
console.log(arr2)
Nothing similar in JavaScript. There might be JavaScript libraries with similar functionality, but I am not aware of any. Simple loop and assignment will be much more efficient, as it avoids the overhead from function calls and making new arrays :
function arraycopy(src, srcPos, dst, dstPos, length) {
while (length--) dst[dstPos++] = src[srcPos++]; return dst;
}
console.log( arraycopy([2,3,4,5,6], 1, [1,1,1,1,1,1], 2, 3) )
Another inefficient alternative for completeness, is copying the values with Object.assign
:
function arraycopy(src, srcPos, dst, dstPos, length) {
return Object.assign(dst, Array(dstPos).concat(src.slice(srcPos, srcPos + length)))
}
console.log( arraycopy([2,3,4,5,6], 1, [1,1,1,1,1,1], 2, 3) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With