copyWithin (array)
  • 23 May 2023
  • Dark
    Light
  • PDF

copyWithin (array)

  • Dark
    Light
  • PDF

Article summary

copyWithin (array) Task Purpose

The copyWithin task is used to copy a portion of an array within the same array. A specified part of an array is copied to a new location in the same array without modifying its size (i.e., length).

Potential Use Case

This task method overwrites the original array by copying array elements to another position in the same array. Suppose you have a list of emails and you want to search to see if there are any copies in the list. You can specify where to start searching in the list (e.g., "0" being the beginning) and then where to end the search. You can also specify where in the email list the results will copy to.

Properties

Input and output parameters are shown below.

Incoming Type Description
arr Array Required. The array to copy.
target Number Required. The index position (destination) to copy the sequence to. The target is a zero based index.
start Number The index position to start copying elements from.
end Number The index position where to stop copying elements.


Outgoing Type Description
copiedArray Array The modified array.

Example

In this IAP example:

  • The arr value is ["a","b","c","d"]. It has four elements.

  • The target index is 2. This means the search begins at the "2" location in the index. Remember that indexing starts at "0" and counts up from there.

  • The start variable of the search is 0, which is the first location in the array being searched.

  • The end variable is 2, indicating the search stops after the first 2 characters in the index and does not copy anything after that.

    copyWithin-ex01

  • The result of this task will make a copy of the first 2 characters |"a"= 0|"b"= 1| and then place that copy in the second character position of the index with |"a"= 2|"b"= 3|. The copiedArray that returns will be ["a","b","a","b"].

    copyWithin-ex02


Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.