Skip to content

Conversation

dwp23
Copy link

@dwp23 dwp23 commented Aug 17, 2025

Description

This PR implements the deepDuplicateArray method in the helpful.js library.
The method allows creating a deep copy of an array, including all nested subarrays, which solves the issue of modifying nested elements affecting the original array.

Issue

Closes #28 – Add deepDuplicateArray method.

Solution

  • Uses the existing duplicateArray method for a shallow copy.
  • Recursively duplicates any subarrays within the array.
  • Returns a fully independent copy of the original array.

Example

let testArray = [1, 2, [3, 4], 5];
let duplicatedArray = helpful.deepDuplicateArray(testArray);

duplicatedArray[2][0] = 99;
console.log(testArray);       // [1, 2, [3, 4], 5]
console.log(duplicatedArray); // [1, 2, [99, 4], 5]

Notes

  • Passing null to the function returns an empty array [].
  • Works recursively for arrays of any depth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

deepDuplicateArray

1 participant