Copy within an Array: `copyWithin()`
Copy within an array: copyWithin()
The copyWithin()
method in JavaScript shallow copies part of an array to another location within the same array, without modifying the arrayβs length.
It allows for in-place modification of the array, making it a useful function for rearranging or manipulating data in arrays without creating new arrays. This method is efficient and destructive, altering the original array.
It accepts up to three parameters, but only the first parameter is mandatory. Hereβs how each parameter works:
-
target
(required): this is the only mandatory parameter. It specifies the index where the copying will begin (the destination index). Elements from another part of the array will be copied here. If the target is negative, it will be counted from the end of the array. -
start
(optional): this is the index from which the copying will start (the source index). If omitted, the copying will start from the beginning of the array (index 0). If negative, it counts from the end of the array. -
end
(optional): this specifies the index where to stop copying (non-inclusive). If omitted, the copying continues until the end of the array. If negative, it also counts from the end of the array.
Example: basic syntax with one parameter
When using one parameter with copyWithin()
, this parameter indicates the target position from which the copied elements will be placed within the same array.
In this case, the target parameter is the index (remember arrays are 0 index base), where the copied elements will start to be placed. The elements will be copied from index 0 (the start of the array) and moved to target
.
Another example:
The initial part of the array is taken and copied from the target
position.
An additional example, take a look at how are filled the 0s values:
From the index 3 to the end of the array, there are 4 elements.
So copyWithin
takes the first four elements of the array and copies them from the element at index 3
Example: basic syntax with two parameters, target
and start
Suppose you have an array of numbers, and you want to create a new array where you want to replace the elements from index 1 using items from index 3. You can use the copyWithin()
method as shown in this example:
Example about basic syntax with three parameters: target
, start
, and end
Another example, suppose you want to create a new array where you want to replace the elements from index 1 using items from 0 to index 3:
Example 3, copyWithin()
allows the use negative values as parameters in this way:
Negative parameters in copyWithin()
Using a negative target
means copying elements starting from the beginning (start
defaults to 0 if not specified) and pasting them starting from a position counted backward from the end.
Explanation:
target
equals to-2
refers to the second-to-last element of the array (index 3). The function copies from the beginning ([10, 20, 30]) and pastes it starting at the third-to-last position, replacing the last two elements with [10, 20].
You can specify a negative start
to begin copying from an index counted backward from the end.
Explanation:
start
equals to-3
refers to the third-to-last element (index 2), which is 30. The function copies from 30 to the end of the array ([30, 40, 50]) and pastes it at index 0, replacing the first three elements.
You can use a negative end
to stop copying at an index counted backward from the end of the array. Note that end
is non-inclusive, meaning it doesnβt include the element at that index.
Explanation:
start
equals to-4
refers to the fourth-to-last element (index 1), which is 20.end
equals to-1
refers to the last element (index 4), but itβs non-inclusive, so copying stops just before 50.- The function copies from 20 to 40 ([20, 30, 40]) and pastes it starting at index 1, but in this case, no values changed as it copied into the same positions.
Combined example: negative target
, start
, and end
Letβs see an example that uses all three parameters as negative values:
Explanation:
target
equals to-3
: This means copying starts at the third-to-last element (index 2).start
equals to-4
: This refers to the fourth-to-last element (b at index 1).end
equals to-2
: This refers to the second-to-last element (d at index 3), but itβs non-inclusive, so copying stops before d.
So, elements from start
= -4 (βbβ) to end
= -2 (βcβ) are copied into positions starting at index -3 (the third-to-last position), resulting in [βaβ, βbβ, βbβ, βcβ, βeβ].
Important considerations
- In-place modification:
copyWithin()
modifies the original array. To keep the original array intact, clone it before using this method. - Shallow copy:
copyWithin()
performs a shallow copy. If the array contains objects, it will only copy references to them, not the objects themselves. - Usefulness in algorithms: This method can be particularly useful in algorithms that require in-place modifications to improve efficiency, such as avoiding creating new arrays when working with large datasets.