Array.from has a second argument (#tilPost)

This post is a rather quick one. I followed a Twitter conversation by Surma from Google and Andrea Giammarchi in which Andrea mentionied that Array.from accepts a second argument. I heard this fact a few times before but always forg…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

This post is a rather quick one. I followed a Twitter conversation by Surma from Google and Andrea Giammarchi in which Andrea mentionied that Array.from accepts a second argument. I heard this fact a few times before but always forgot it again. Let's hope I'll remember this fact the next time I want to create and transform an Array out of something.

console.log(Array.from([1, 2, 3], x => x + x));
// [2, 4, 6]

The snippet they discussed showed functionality to call a callback X times.

// call a callback `length` times
Array.from({length}, () => callback())

// or generate a random emoji Array
Array.from(
  {length: 7},
  (v, i) => String.fromCodePoint(
    129300 + Math.floor(Math.random() * 20)
  )
);

// [ '?', '?', '?', '?', '?', '?', '?' ]

You can read more about Array.from on MDN.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2019-05-08T22:00:00+00:00) Array.from has a second argument (#tilPost). Retrieved from https://www.scien.cx/2019/05/08/array-from-has-a-second-argument-tilpost/

MLA
" » Array.from has a second argument (#tilPost)." Stefan Judis | Sciencx - Wednesday May 8, 2019, https://www.scien.cx/2019/05/08/array-from-has-a-second-argument-tilpost/
HARVARD
Stefan Judis | Sciencx Wednesday May 8, 2019 » Array.from has a second argument (#tilPost)., viewed ,<https://www.scien.cx/2019/05/08/array-from-has-a-second-argument-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Array.from has a second argument (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2019/05/08/array-from-has-a-second-argument-tilpost/
CHICAGO
" » Array.from has a second argument (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2019/05/08/array-from-has-a-second-argument-tilpost/
IEEE
" » Array.from has a second argument (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2019/05/08/array-from-has-a-second-argument-tilpost/. [Accessed: ]
rf:citation
» Array.from has a second argument (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2019/05/08/array-from-has-a-second-argument-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.