const emptyRow = 3;
How can I map on this if it’s a number and not an array?
something like:
emptyRow.map(el => {
return (
<div>place items here</div>
)
})
The purpose to this is that I would like to create divs dynamically based on the number of emptyRows.
4
Answers
For this you can just use
for
loopHope this helps…
Doesn’t really work that way… you seem like maybe you’re confusing
map
withrepeat
, which I believe only works on strings.What you can do is create a range. Javascript doesn’t have a native
range
function, but you can write it pretty easily:Not pretty, but it’s simple and it works.
A slightly more elegant and concise way to achieve this is by using
Array.from()
directly, which avoids the need to usefill()
:Why
Array.from()
?No need for
fill(0)
:Array.from()
allows you to directly specify the length of the array and map over it at the same time.Concise: It reduces the amount of code and increases readability by combining array creation and mapping into a single function.
This will create the desired number of
<div>
elements dynamically, and you can adjust the number inemptyRows
as needed. Plus, it’s easy to understand at a glance!