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?
index ### returns the index(location of item in an array) of the current element. array. length - 1 ### gives us the length of the array and - 1 gives us the index of the last element in the array.
To get the last item without knowing beforehand how many items it contains, you can use the length property to determine it, and since the array count starts at 0, you can pick the last item by referencing the <array>. length - 1 item.
Use the array. prototype. splice() to Remove the Last Element From an Array JavaScript. The splice() method is used to change the array by removing or replacing the elements.
Definition and Usage. map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.
Try something like:
row.map((rank, i, row) => { if (i + 1 === row.length) { // Last one. } else { // Not last one. } })
Old answer:
const rowLen = row.length; row.map((rank, i) => { if (rowLen === i + 1) { // last one } else { // not last one } })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With