Javascript offers at least 4 ways to fill an array with different values using only native language methods. These methods allow you to create an arrangement of a predefined size and content.
It's possible to use Array.fill
to create an Array with a pre-defined size and then use Array.map
to populate the items with a default value, like integer or string.
It's also possible to populate the array with an object, but we will require to use another function because Array.fill
just copy a reference to the original object without creating multiple unique objects. To solve this we can use Array.map
.
Other way to solve the same problem is by using a for loop, but this is a mutable method.
Also, we can use Array.from
or the array spread syntax in combination with the Array.map
function.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!