JavaScript tips — Initialize an array with a given value using Array.fill

Publié le: 25 octobre 2022
sur la chaîne: Code 2020
194
5

Need to create an array of a given size where all the entries in it have been initialized to a given number or string value? Just use the new Array constructor and the fill method:

new Array(size).fill(initialValue)

This also works for typed arrays, such as Uint8Array. It is much more concise (and faster) than doing this with a for loop yourself

#javascript


Sur cette page du site, vous pouvez voir la vidéo en ligne JavaScript tips — Initialize an array with a given value using Array.fill durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Code 2020 25 octobre 2022, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 194 fois et il a aimé 5 téléspectateurs. Bon visionnage!