title: JavaScript split string by comma and character count
body: <p>I have a string that contains about 10,000 ids comma separated and I would like to split them by commas plus make sure that the character count in each one of them doesn't exceed 1500 characters.</p>
  
  <p>For E.g.: </p>
  
  <pre><code>var str = "12345, 567789, 435345, 55446444564, 786756, 33322 .... ";
  var commaSeperatedSplitArr = str.split(",");
  var arrayOfArrays = [];
  var limit = 50;
  
  // So, I would like to get my commaSeperatedSplitArr[0].length &gt;= 1500 and &lt;= 1600 characters such that none of the Ids are cropped off    
  while(commaSeperatedSplitArr.length &gt; 0) {
      var splicedCharCount = arrayOfEmails.splice(0, limit)[0].length;
      if(splicedCharCount &gt;= 1500 &amp;&amp; splicedCharCount &lt;= 1600) {
          arrayOfArrays.push(commaSeperatedSplitArr.splice(0, limit));  
      }
  }
  </code></pre>
  
  <p>What would be the best way to achieve this? I have tried using str.splice(0, limit) but that just gives me for the count of limit rather than the number of characters I want.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Also, how can we use the above approach to apply for a list of email addresses?</p>
  
  <pre><code>For E.g.: var str = "abc@gmail.com, xyz@help-desk.com, hello@world.com.... "
  </code></pre>
  
  <p><strong>Expected Result:</strong>
  1) For Ids: (if limit is 50 characters)</p>
  
  <pre><code>arrayOfArrays[0] = "12345, 567789, ... "; (should contain 50 characters or less without any id being cropped off.)
  </code></pre>
  
  <p>2) For Email Addresses: (if limit is 50 characters)</p>
  
  <pre><code>arrayofArrays[0] = "abc@gmail.com, xyz@help-desk.com,..."; (should contain 50 characters or less without any email address being cropped off.)
  </code></pre>
  
  <p>Looking forward to your solution guys.
  Cheers.</p>