Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function to copy set of elements within a range in an array into a range in another array?

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

like image 976
Harish Kayarohanam Avatar asked Aug 28 '18 23:08

Harish Kayarohanam


People also ask

How do I copy an array from one range to another?

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.

How to copy one element at a time in an array?

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?

What is the difference between arraycopy () and clone ()?

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.

How do you make a copy of a source 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.


2 Answers

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)
like image 76
CRice Avatar answered Oct 19 '22 23:10

CRice


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) )
like image 41
Slai Avatar answered Oct 19 '22 23:10

Slai