Estoy intentando hacer una función que duplique una matriz de matrices. Intenté blah.slice (0); pero solo copia las referencias. Necesito hacer un duplicado que deje el original intacto.Cómo copiar o duplicar una matriz de matrices
me encontré con este método prototipo en http://my.opera.com/GreyWyvern/blog/show.dml/1725165
Object.prototype.clone = function() {
var newObj = (this instanceof Array) ? [] : {};
for (i in this) {
if (i == 'clone') continue;
if (this[i] && typeof this[i] == "object") {
newObj[i] = this[i].clone();
} else newObj[i] = this[i]
} return newObj;
};
Funciona, pero puede confundir un plugin de jQuery que estoy usando - por lo que necesito para convertirlo en una función ... y recursividad no es mi más fuerte
¡Su ayuda sería apreciada!
Saludos,
Asegúrese de declarar la "i" con 'var'! También es riesgoso iterar sobre una matriz con un bucle 'for ... in', mucho más seguro usar índices numéricos. – Pointy
Ver: http://stackoverflow.com/questions/565430/deep-copying-an-array-using-jquery –