Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  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. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement