I need to create an array of arrays. I had a pretty extensive if/else cycle working, but I think I can refactor it down using jQueries nextUntil function. Based on what I'm seeing, I'm on the right track, but my results are just shy of what I need in both directions.
Here's the HTML
var array = [[]];
<ul id="list">
<li class="get-it">1</li>
<li class="get-it">2</li>
<li class="ignore-it">3</li>
<li class="get-it">4</li>
<li class="get-it">5</li>
<li class="ignore-it">6</li>
<li class="get-it">7</li>
<li class="get-it">8</li>
<li class="ignore-it">9</li>
<li class="get-it">10</li>
<li class="get-it">11</li>
</ul>
And here's a couple of ways I've tried to scrape the .get-it list items
// returns single-item array of all .get-it items
array.push( $( '#list' ).children().nextUntil( '.ignore-it' );
// returns 8 single-item array of .get-it items
$row.children().nextUntil( '.ignore-it' ).each(function(){
array.push( $(this) );
});
Here's what I actually need returned
// what I need returned
array = [
[ li[0], li[1] ],
[ li[3], li[4] ],
[ li[6], li[7] ],
[ li[9], li[10] ]
]
This will work:
var array = $('.get-it:first, .ignore-it + .get-it').map(function() {
return $(this).add($(this).nextUntil('.ignore-it'));
}).get();
Basically it works like this:
// grab the first .get-it, as well as all .get-it
// elements that follow an .ignore-it element
$('.get-it:first, .ignore-it + .get-it')
// map each of those items into an array that contains
// itself, plus all .get-it elements up until the next
// .ignore-it element
.map(function() {
return $(this).add($(this).nextUntil('.ignore-it'));
})
// convert our jQuery collection into a regular array
.get();