How to Clone a (Nested) Array in Javascript

The Problem David Walsh has a quick tutorial on how to clone an array in Javascript: var clone = myArray.slice( 0 ); This has an unmentioned danger. If your array contains nested arrays, the "clone" will contain references to the old arrays. Observe: var original = [ [1] ]; var cloned = original.slice( 0…