Created
September 30, 2021 18:13
-
-
Save sagrawal31/4c5b15f6b1757a835ee34310febeb42d to your computer and use it in GitHub Desktop.
Shorten your code in JavaScript/TypeScript to make it readable & clean https://www.linkedin.com/feed/update/urn:li:activity:6849404507500154880/ & https://twitter.com/snagrawal11/status/1443637925099409408
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
const urls: string[] = []; | |
const gallery = [ | |
{id: 1, url: 'http://example.com/1.png'}, | |
{id: 2, url: 'http://example.com/2.png'} | |
]; | |
// Long code | |
gallery.forEach((item) => { | |
urls.push(item.url); | |
}); | |
// Short code | |
urls.push(...gallery.map(item => item.url)); | |
// "...” Spreat operator in JavaScript/TypeScript | |
// “map” function in JavaScript/TypeScript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment