Design & Download
Your JS Library
in Seconds!!!

Snippet: copyArray() - Deep Copying Arrays

Written by Christopher West (cwest) on December 04, 2015.
var copyArray;
(function() {
  function helper(arr, pairs, i, t, copy) {
    for (i = pairs.length; t = pairs[--i];) {
      if (t.o === arr) {
        return t.c;
      }
    }

    pairs.push({ o: arr, c: copy = slice(arr) });

    for (i = copy.length; i--;) {
      if (typeOf(copy[i], 'Array')) {
        copy[i] = helper(arr[i], pairs);
      }
    }

    return copy;
  }
  copyArray = function(arr) {
    return helper(arr, []);
  };
})();