Created
August 15, 2011 12:47
-
-
Save juandopazo/1146157 to your computer and use it in GitHub Desktop.
Concatenar arrays anidados sin recursividad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function aplaneitor2_ES3(arr) { | |
arr = arr.concat(); //devolver un nuevo array, no modificar el mismo | |
var i = 0; | |
while (i < arr.length) { | |
if (Object.prototype.toString.call(arr[i]) == '[object Array]') { | |
Array.prototype.splice.apply(arr, [i, 1].concat(arr[i])); | |
} else { | |
i++; | |
} | |
} | |
return arr.join('+'); | |
} | |
var test = ["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengo", ["dinero"] ] ]; | |
console.log(aplaneitor2_ES3(test)); // 'hola+soy+juan+fernandez+y+no+tengo+dinero' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
¡Bravo!