title: Looking for a Cleaner Way To Make this Pagination Function in JavaScript
body: <p>I want to write a method that takes in an offset and limit along with a passed in Array of objects.  The objects always have an id column.  I want to return a new Array with the result based on the offset and limit. Here is an example, followed by my implementation that I'm not happy with (too many fences and fence post like variables which are always error prone). Also, if limit == -1 then take the rest.</p>
  <p>Maybe there is a better way with slice?  Could reduce somehow help?</p>
  <pre><code>const baseArray = [{name: 'peter'},{name: 'terry'},{name: 'tammy'},{name: 'mary'}];
  const offset = 1;
  const limit = 2;
  const speakerArray = getPaginatedData(baseArray,offset,limit);
  
  speakerArray is [{name: 'terry',cursor: 'dGVycnk='},{name: 'tammy', cursor: 'dGFtbXk='];
  </code></pre>
  <p>where, the cursor's are calculated with this line of code:</p>
  <pre><code>console.log(Buffer.from('terry').toString(&quot;base64&quot;));
  </code></pre>
  <p>Here is my implementation I don't like.</p>
  <pre><code>  const speakerArray = baseArray
      .filter((rec, index) =&gt; {
        return index &gt; offset - 1 &amp;&amp; (offset + limit &gt; index || limit == -1);
      })
      .map((rec) =&gt; {
        rec.cursor = Buffer.from(rec.name.toString()).toString(&quot;base64&quot;);
        return rec;
      });
  </code></pre>