I have an array with items which can have even or odd number of items. I made a function which puts each two elements in their own array inside a main array, so it looks like this at the end;
items array: [{}, {}, {}, {}]
returned array: [[{}, {}], [{}, {}]]
items array: [{}, {}, {}]
returned array: [[{}, {}], [{}, undefined]]
I did this because I want to render each Bootstrap row with two columns on the page. Now, I'm not sure how to implement mapping through this array. To a certain extent I know how to do this;
- Map through returned array.
- If second element in current array is
undefinedreturn a row with just one item. - If both elements are defined in current array return a row with both items.
But by React rules I need to add additional key attributes to the elements I return with map so I would need to somehow keep the track of index variable. What would be the efficient way to do this?
I did this because I want to render each Bootstrap row with two columns on the page.I personally think your going about this the wrong way, bootstrap is a responsive layout engine, you shouldn't be having to hard code column breaks,. just set yourcol-md-6 cols-xs-12etc grid rules.