SHARE
TWEET

Untitled

a guest Apr 19th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* decided to use ES5 */
  2. /* added it to the array prototype although is not recomended for a prod release, just as a example */
  3. (function(proto) {
  4.   function isArray(val) {
  5.     return (Object.prototype.toString.call(val) === '[object Array]') ? true : false;
  6.   }
  7.  
  8.   function flat(a,b) {
  9.     return a.concat(isArray(b) ? b.reduce(flat, []) : [b]);
  10.   }
  11.  
  12.   proto.flatten = function() {
  13.     return this.reduce(flat, [])
  14.   }
  15. })(Array.prototype)
  16.  
  17. /* use
  18.  * [43,3,[3,35,64,[34,3,[433]]]].flatten();
  19.  */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top