I have this:
map = ranks.map((row, r) => (
row.map((rank, i) => {
return [element(r, i, state, rank, toggled, onClick)];
})
));
It maps through a 2-dimentional array.
After each row, I'd like to insert <div class="clearfix"></div>
.
I think, if I could somehow get the last index for each row, so I will be able to use it in the row map callback. Can someone show me how to do it?
A slight improvement on the accepted answer:
This removes the arithmetic from inside the loop.
As LeoYuan 袁力皓 answered, this is the correct answer, but it can be a bit improved.
map
accepts a function with a third parameter, which is the iterated array itself.Using an
arr.length
instead ofrow.length
is a better and correct approach for several reasons:When you like to provide an explicit array, it will work as well. E.g.
If you like to move the callback outside of the
map
scope (mainly for a better performance), it will be wrong to userow.length
as it is out of scope. E.g. in the OP case:Will return
apple, orange, banana.
you can check last index with your array's length. here is a logic
simplify answer above