// better toString, helps with flatten Array.prototype.toString = function() { var result = "["; for (var i = 0; i < this.length; i++) { if (i > 0) { result += ", "; } result += this[i]; } return result + "]"; }; Array.prototype.without = function() { var result = []; for (var i = 0; i < this.length; i++) { var found = false; for (var j = 0; j < arguments.length; j++) { if (this[i] === arguments[j]) { found = true; break; } } if (!found) { result.push(this[i]); } } return result; }; var wot1 = [2, 6, 4, 8, 4, 9, 7, 1, 2, 9, 4]; // .without(4, 2, 7) var wot2 = [10, 10, 20, 20, 10, 20]; // .without(10, 20) var wot3 = ["a", "b", "c", "b", "e", "c", "d"]; // .without("b", "x", "z", "y", "f", "e") String.prototype.isIpAddress = function() { return !!this.match(/^(\d{1,3}\.){3}\d{1,3}$/); }; var ip1 = "192.168.1.64"; var ip2 = "10.0.0.1"; var ip3 = "128.208.3.88"; var ip4 = "hi 1.2.3.4 bye"; var ip5 = "eat at joe's"; var ip6 = "1 2 3 4 ."; var ip7 = "9999.9999.9999.9999"; var ip8 = "123 123 123 123"; var ip9 = "1.2.3"; Array.prototype.flatten = function() { var result = []; for (var i = 0; i < this.length; i++) { if (typeof(this[i]) === "object" && this[i].length) { var flat = this[i].flatten(); result = result.concat(flat); } else { result.push(this[i]); } } return result; }; Array.prototype.flatten = function() { var result = []; function helper(element) { if (element instanceof Array && element.length !== undefined) { for (var i = 0; i < element.length; i++) { helper(element[i]); } } else { result[result.length] = element; } } helper(this); return result; }; Array.prototype.flatten = function() { var result = []; function help(n) { if (typeof(n) == "object" && typeof(n.length) != "undefined") { if (n.length > 0) { n.map(help); // or forEach } } else { result.push(n); } } help(this); return result; }; Array.prototype.flatten = function() { var y = []; function flat(x) { x.forEach(function(z) { if (typeof(z) === "object" && typeof(z.length) === "number") { flat(z); } else { y.push(z); } }); } flat(this); return y; }; Array.prototype.flatten = function() { if (this.length === 0) { return []; } else if (this[0].length !== undefined) { return this[0].flatten().concat(this.slice(1, this.length).flatten()); } else { return [this[0]].concat(this.slice(1, this.length).flatten()); } }; Array.prototype.flatten = function() { var f = function(out, lst) { for (var i = 0; i < lst.length; i++) { if (typeof(lst[i]) === "object" & lst[i].length !== undefined) { f(out, lst[i]); } else { out[out.length] = lst[i]; } } return out; }; return f([], this); }; var flattest1 = [1, 2, [3, 4], 5, [6, [7, 8, [9, 10], 11]], [], [], [[[[[[12], 13]]]]]];