Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Forked from juandopazo/gist:1146157
Created August 15, 2011 14:06
Show Gist options
  • Save joseanpg/1146822 to your computer and use it in GitHub Desktop.
Save joseanpg/1146822 to your computer and use it in GitHub Desktop.
Concatenar arrays anidados sin recursividad
function esArray(array) {
return Object.prototype.toString.call(array) == '[object Array]');
}
function desempaqueta(array,index) {
Array.prototype.splice.apply(array, [index, 1].concat(array[index]));
}
function flatten(arr) {
arr = arr.concat();
var i = 0;
while (i < arr.length)
if ( esArray(arr[i])) desempaqueta(arr,i);
else i++;
return arr;
}
var test = ["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengo", ["dinero"] ] ];
console.log(flatten(test));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment