flatten
JavaScript
/*
Array.prototype.flatten || (Array.prototype.flatten = function() {
return this.reduce(function(a, b) {
return (b instanceof Array)?
a.concat(b.flatten()):
a.concat(b);
}, []);
});*/
Array.prototype.flatten || (Array.prototype.flatten = function() {
return this.reduce(function(a, b) {
return a.concat((b instanceof Array) ? b.flatten() : b);
}, []);
});
var a = [1, 2, 3, [4, 5, [6, 7, [8, 9], 10]]].flatten();
var animals = ['pigs', 'goats', 'sheep'];
animals.push
document.write(a.length + " / "); //9
document.write(typeof a + " / "); //object
document.write((a instanceof Array) + " / " + "<br />"); //true
for (var i = 0, l = a.length; i < l; ++i) {
document.write(i + " : " + a[i] + "<br />");
}