Skip to content

Instantly share code, notes, and snippets.

@listenrightmeow
Created February 8, 2019 22:12
Show Gist options
  • Save listenrightmeow/5722322b0e896cbbd58cb30b6b3a6918 to your computer and use it in GitHub Desktop.
Save listenrightmeow/5722322b0e896cbbd58cb30b6b3a6918 to your computer and use it in GitHub Desktop.
simple array flat prototype
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