title: Can I $.wrap() around a collection of elements in an array?
body: <p>Let's say I have a collection of 'items' like so:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;p class="item"&gt;Item 1&lt;/p&gt;
  &lt;p class="item"&gt;Item 2&lt;/p&gt;
  &lt;p class="item group"&gt;Item 3&lt;/p&gt;
  &lt;p class="item group"&gt;Item 4&lt;/p&gt;
  &lt;p class="item"&gt;Item 5&lt;/p&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I want to loop through the items and wrap a containing div around any that have the 'group' class to result in something like this (grouped items will <strong>always</strong> be right next to one another):</p>
  
  <p>This is the script I've got:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var group = [];
  
  $('.item').each(function(i, item) {
    if ($(item).hasClass('group')) {
      group.push(item);
    }
  });
  
  $(group).wrap('&lt;div class="wrapper" /&gt;');</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  
  &lt;p class="item"&gt;Item 1&lt;/p&gt;
  &lt;p class="item"&gt;Item 2&lt;/p&gt;
  &lt;div class="wrapper"&gt;
    &lt;p class="item group"&gt;Item 3&lt;/p&gt;
    &lt;p class="item group"&gt;Item 4&lt;/p&gt;
  &lt;/div&gt;
  &lt;p class="item"&gt;Item 5&lt;/p&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>What happens is that the wrapping div is wrapped around each element separately in the array (which makes sense) but I need it to wrap all elements together. Is there any way I can do this? Here's a <a href="http://jsfiddle.net/LanMt/2/" rel="nofollow noreferrer">jsFiddle</a>.</p>
  
  <p>There is a more complex variation of this problem that is possible, this would be a situation where there are several 'sets' of these groups, each to be wrapped in their own 'group' <code>div</code>. Initial state:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;p class="item"&gt;Item 1&lt;/p&gt;
  &lt;p class="item"&gt;Item 2&lt;/p&gt;
  &lt;p class="item group"&gt;Item 3&lt;/p&gt;
  &lt;p class="item group"&gt;Item 4&lt;/p&gt;
  &lt;p class="item"&gt;Item 5&lt;/p&gt;
  &lt;p class="item group"&gt;Item 6&lt;/p&gt;
  &lt;p class="item group"&gt;Item 7&lt;/p&gt;
  &lt;p class="item group"&gt;Item 8&lt;/p&gt;
  &lt;p class="item"&gt;Item 9&lt;/p&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Desired state:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;p class="item"&gt;Item 1&lt;/p&gt;
  &lt;p class="item"&gt;Item 2&lt;/p&gt;
  &lt;div class="wrapper"&gt;
    &lt;p class="item group"&gt;Item 3&lt;/p&gt;
    &lt;p class="item group"&gt;Item 4&lt;/p&gt;
  &lt;/div&gt;
  &lt;p class="item"&gt;Item 5&lt;/p&gt;
  &lt;div class="wrapper"&gt;
    &lt;p class="item group"&gt;Item 6&lt;/p&gt;
    &lt;p class="item group"&gt;Item 7&lt;/p&gt;
    &lt;p class="item group"&gt;Item 8&lt;/p&gt;
  &lt;/div&gt;
  &lt;p class="item"&gt;Item 9&lt;/p&gt;</code></pre>
  </div>
  </div>
  </p>