Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* decided to use ES5 */
- /* added it to the array prototype although is not recomended for a prod release, just as a example */
- (function(proto) {
- function isArray(val) {
- return (Object.prototype.toString.call(val) === '[object Array]') ? true : false;
- }
- function flat(a,b) {
- return a.concat(isArray(b) ? b.reduce(flat, []) : [b]);
- }
- proto.flatten = function() {
- return this.reduce(flat, [])
- }
- })(Array.prototype)
- /* use
- * [43,3,[3,35,64,[34,3,[433]]]].flatten();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement