If you are lucky enough to have a NetScape browser derivative (FF etc.)
resultCopy = eval ( uneval( result ) );
or
resultCopy = eval ( result . toSource( ) );
otherwise use (this has severe limitations but meets and satisfies the OP conditions):
resultCopy = eval( "[ [" + result . join("], \n[") + "] ]" );
The eval
function is "builtin" hiding the recursive nature of its algorithm.
If re-cursing (ie. recursion) is sacrosanct, the following is deeply religious having no iterative statements and recursing completely until each element is not an array. (ie. recursion occurs while an element is an array or is in an array). Given an array, if it is vacuous, then it's copy will be vacuous array.length < 1 ? [ ]
. If not take an element from the array reducing the array to a smaller one that needs to be processed further. This is the recursive reduction step RAffle ( array . pop( ), arbitArrayCopy ( array ) )
. The new array is filled with the element
that was removed nuRA . push ( ... )
. If the element
happens to be an array then process it further ie. re-curse element instanceof Array ? arbitArrayCopy ( element )
. This algorithm is destructive and the original array is to be unblemished, so restore it: array . push ( element )
.
function arbitArrayCopy ( array ) {
/* copies any nD array structure and primitive array elements but
complex compound object array elements are referenced and not copied */
return array.length < 1 ? [ ] : RAffle ( array . pop( ), arbitArrayCopy ( array ) ) ;
function RAffle ( element, nuRA ) {
nuRA . push ( element instanceof Array ? arbitArrayCopy ( element ) : element ) ;
array . push ( element ) ;
return nuRA
}
}
Test:
data:text/html;charset=utf-8,<html>
<!-- this is a scriple or scURIple which codes generic URI's of arbitrary schema
- javascript: schema specific scriples/scURIples are known as scriplets or bookmarklets -->
<script>
javascript:
function RAcopy ( array ) { return array.length < 1 ? [ ] : RAffle( array . pop( ), RAcopy ( array ) ) ;
function RAffle ( element, nuRA ) {
nuRA . push ( element instanceof Array ? RAcopy ( element ) : element ) ;
array . push ( element ) ; return nuRA
} }
result = [[8,2,5],[3],[8,5,2],[0]];
RAdup = RAcopy ( resultCopy = eval( "[ [" + result . join("], \n[") + "] ]" ) ) ;
resultCopy[2][0] = 9; resultCopy[3] = [10,11,12]; RAdup[1]=[ 42, 33, 24 ]; delete RAdup[3];
alert ( [ "Test environment:\n " + window.navigator.userAgent,
"RAdup =\n" + RAdup . join("], \n[") ,
"resultCopy =\n" + resultCopy . join("], \n[") ,
"result =\n" + result . join("], \n[") ] . join("\n ..................... \n") );
</script></html>
(Caveat: scURIples are polyglot challenged due to the schizo lingua vagaries of the concurrent URI, javascript and HTML syntaxes but convenient for immediate mode address bar drag & drop execution - the symbiotic synergy of their syzygy often succumbs to parasitism - fortunately, here the forces are aligned - in particular the HTML <script>
intentionally includes a javascript:
label to make the script a valid URI scriplet via the javascript: schema/protocol and all this is embedded in the larger URI data: scheme scURIple - so the data: URI scURIple encodes valid HTML which encodes valid javascript which encodes a valid javascript: URI scriplet - things get really sqURIlly when the script generates other scURIples or even itself)
Test results:
Test environment:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
(Splashtop-v1.2.17.0)
.....................
RAdup =
8,2,5],
[42,33,24],
[8,5,2],
[
.....................
resultCopy =
8,2,5],
[3],
[9,5,2],
[10,11,12
.....................
result =
8,2,5],
[3],
[8,5,2],
[0
link:
Algorithm for copying 2 dimensional array in javascript recursively