Created
February 8, 2019 22:12
-
-
Save listenrightmeow/5722322b0e896cbbd58cb30b6b3a6918 to your computer and use it in GitHub Desktop.
simple array flat prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.defineProperty(Array.prototype, 'flat', { | |
value: function(res = []) { | |
Object(this).forEach(cursor => { | |
if (Object.prototype.toString.call(cursor) === '[object Array]') { | |
cursor.flat(res); | |
} else { | |
res.push(cursor); | |
} | |
}); | |
return res; | |
} | |
}) | |
console.log([[1,2,[3]],4].flat(), 'flat'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment