From b3e984e2bb04c98ee5bec184b594ee4cc1f1194d Mon Sep 17 00:00:00 2001 From: dwp23 Date: Sun, 17 Aug 2025 11:30:18 +0200 Subject: [PATCH] enhancement: implement deepDuplicateArray (closes #28) --- helpful.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/helpful.js b/helpful.js index d39db91..5c47cb7 100644 --- a/helpful.js +++ b/helpful.js @@ -31,6 +31,31 @@ return duplicated; } + + helpful.deepDuplicateArray = function(array) { + if (array == null) { + return []; + } + + let duplicated = helpful.duplicateArray(array); // flache Kopie + for (let i = 0; i < duplicated.length; i++) { + if (Array.isArray(duplicated[i])) { + duplicated[i] = helpful.deepDuplicateArray(duplicated[i]); // Rekursion bei Subarrays + } + } + return duplicated; +} + + console.log(duplicatedArray); + +testArray[2][0] = 99; +console.log(testArray); // Sollte [1, 2, [99, 4], 5] ausgeben +console.log(duplicatedArray); + + console.log(helpful.deepDuplicateArray(null)); + + + helpful.differenceOfArrays = function(array1, array2) { if(array1 == null || array2 == null) { return [];