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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.