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
APA
Stefan Judis | Sciencx (2023-01-27T01:28:44+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 2023-01-27T01:28:44+00:00,<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 2023-01-27T01:28:44+00:00]. 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 2023-01-27T01:28:44+00:00. 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: 2023-01-27T01:28:44+00:00]
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/ | 2023-01-27T01:28:44+00:00
https://github.com/addpipe/simple-recorderjs-demo