Creating an Empty Array in Javascript
I needed it recently, for example I have a selectbox to list the days of the month from 1-31, but I needed to create an array with numbers from 1 to 31.
If I create it like this;
new Array(31)
but I have to fill it myself. Instead, a solution like this would make more sense;
const numbers = Array.from({ length: 31 }, (_, i) => i + 1)
console.log(numbers)
now you have an array with numbers from 1 to 31, I usually use it to fill in the select in react projects, it will definitely be useful for you one day :)
Comments
Popular Articles
Popular Tags
- #directive
- #Function
- #Json
- #Class
- #SASS
- #Form
- #Redirect
- #API
- #Special Characters
- #Special
- #Random
- #Generating
- #
- #Text
- #Ajax
- #URL
- #Encrypting
- #React
- #Show
- #timeAgo
- #PSR-4
- #Hide
- #DDOS Protection
- #DDOS
- #cURL
- #Logic For Displaying Posts
- #Error
- #Key
- #General Error
- #1364 Field
- #Abbreviation
- #Blade
- #Version
- #QR
- #QR Code Generating
- #Array
- #Arrays with Key Values to String Statement
- #Short Tag
- #Activate
- #Real Time
- #Socket.io
- #301
- #Custom Directives
- #Iframe Detection
- #Date
- #Characters
- #Insert
- #Autoloader
- #Composer
- #Reading
There are no comments, make the firs comment