I currently have some code that does this:
let result = []
const number
for (let i = 0; i < number; i++) {
result.push(f(i))
result.push(g(i))
}
return (
<div>
{result}
</div>
)
f(i) and g(i) return a single react jsx element depending on what i is. We will pass in i as a prop.
I am having trouble converting this to a form that does not use a for loop. Is there a way to use map? I would need there to be no additional div wrapped around f(i) and g(i). In other words I CANNOT have the result equivalent to this
for (let i = 0; i < number; i++) {
result.push(
<div>
f(i)
g(i)
</div>
)
}
The requirement of having no additional div makes it a bit hard for me to come up with a map solution or any other solution that does not use a for loop. Do you have any ideas?
result.push([f(i), g(i)]).mapis nice when what you want is a transformed array, but you really seem to need custom behaviour for each element in an array - this is what for loops are for, so I say stick with the loop. As a bonus, they are more efficient than.map, especially if you're going to have to be really clever to make.mapwork.