My goal is to create a list with alternating classes that have a start and end class for each set of items. For example, I currently have the following code:
<div class="wrap">
<div class="item-a"></div>
<div class="item-a"></div>
<div class="item-a"></div>
<div class="item-a"></div>
<div class="item-b"></div>
<div class="item-b"></div>
<div class="item-b"></div>
<div class="item-a"></div>
<div class="item-a"></div>
<div class="item-a"></div>
<div class="item-b"></div>
<div class="item-b"></div>
</div>
I want to achieve the following structure:
<div class="wrap">
<div class="item-a start"></div>
<div class="item-a"></div>
<div class="item-a"></div>
<div class="item-a end"></div>
<div class="item-b start"></div>
<div class="item-b"></div>
<div class="item-b end"></div>
<div class="item-a start"></div>
<div class="item-a"></div>
<div class="item-a end"></div>
<div class="item-b start"></div>
<div class="item-b end"></div>
</div>
I found a solution involving jQuery, but I need to accomplish this using plain JavaScript as I do not have jQuery in my environment.
In my React project, I am working with an array of objects retrieved from an API like this:
arr = [
{class:"left", id:"0", direction:0 },
{class:"right", id:"1", direction:1 },
{class:"right", id:"2", direction:1 },
{class:"left", id:"3", direction:0 },
{class:"left", id:"4", direction:0 },
{class:"left", id:"5", direction:0 },
{class:"right", id:"6", direction:1 },
{class:"right", id:"7", direction:1 },
]
I have attempted to use a for loop to achieve this, but so far have been unsuccessful.