UNPKG

lodash-contrib

Version:

The brass buckles on lodash's utility belt

700 lines (495 loc) 33.4 kB
<!DOCTYPE html> <html> <head> <title>_.array.builders.js.md</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul id="jump_to"> <li> <a class="large" href="javascript:void(0);">Jump To &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page_wrapper"> <div id="jump_page"> <a class="source" href="_.array.builders.js.html"> _.array.builders.js.md </a> <a class="source" href="_.array.selectors.js.html"> _.array.selectors.js.md </a> <a class="source" href="_.collections.walk.js.html"> _.collections.walk.js.md </a> <a class="source" href="_.function.arity.js.html"> _.function.arity.js.md </a> <a class="source" href="_.function.combinators.js.html"> _.function.combinators.js.md </a> <a class="source" href="_.function.iterators.js.html"> _.function.iterators.js.md </a> <a class="source" href="_.function.predicates.js.html"> _.function.predicates.js.md </a> <a class="source" href="_.object.builders.js.html"> _.object.builders.js.md </a> <a class="source" href="_.object.selectors.js.html"> _.object.selectors.js.md </a> <a class="source" href="_.util.existential.js.html"> _.util.existential.js.md </a> <a class="source" href="_.util.operators.js.html"> _.util.operators.js.md </a> <a class="source" href="_.util.strings.js.html"> _.util.strings.js.md </a> <a class="source" href="_.util.trampolines.js.html"> _.util.trampolines.js.md </a> <a class="source" href="index.html"> index.md </a> </div> </div> </li> </ul> <ul class="sections"> <li id="title"> <div class="annotation"> <h1>_.array.builders.js.md</h1> </div> </li> <li id="section-1"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-1">&#182;</a> </div> <h3 id="array-builders">array.builders</h3> <blockquote> <p>Functions to build arrays. <a href="_.array.builders.js.html" class="btn btn-primary btn-xs">View Annotated Source</a></p> </blockquote> <hr> </div> </li> <li id="section-2"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-2">&#182;</a> </div> <h4 id="cat">cat</h4> <p>Signature: <code>_.cat(... arrays:Array ...)</code></p> <p>The <code>_.cat</code> function provides a way to concatenate zero or more heterogeneous arrays into one.</p> <pre><code class="lang-javascript">_.cat(); <span class="hljs-comment">// 0-args</span> <span class="hljs-comment">//=&gt; []</span> _.cat([]); <span class="hljs-comment">// 1-arg, empty array</span> <span class="hljs-comment">//=&gt; []</span> _.cat([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]); <span class="hljs-comment">// 1-arg</span> <span class="hljs-comment">//=&gt; [1,2,3]</span> _.cat([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>],[<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>]); <span class="hljs-comment">// 2-args</span> <span class="hljs-comment">//=&gt; [1,2,3,4,5,6]</span> _.cat([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>],[<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>],[<span class="hljs-number">7</span>]); <span class="hljs-comment">// 3+ args</span> <span class="hljs-comment">//=&gt; [1,2,3,4,5,6,7]</span> </code></pre> <p>Not every argument to <code>_.cat</code> needs to be an array; other types are accepted.</p> <p>Signature: <code>_.cat(... elems:Any ...)</code></p> <pre><code class="lang-javascript">_.cat(<span class="hljs-number">1</span>,[<span class="hljs-number">2</span>],<span class="hljs-number">3</span>,<span class="hljs-number">4</span>); <span class="hljs-comment">// mixed args</span> <span class="hljs-comment">//=&gt; [1,2,3,4]</span> </code></pre> <p>The <code>_.cat</code> function will also work with the <code>arguments</code> object as if it were an array.</p> <p>Signature: <code>_.cat(... elems:Arguments ...)</code></p> <pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params"></span>)</span>{ <span class="hljs-keyword">return</span> _.cat(<span class="hljs-built_in">arguments</span>, <span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>); } f(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>); <span class="hljs-comment">//=&gt; [1,2,3,4,5,6]</span> </code></pre> <hr> </div> </li> <li id="section-3"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-3">&#182;</a> </div> <h4 id="chunk">chunk</h4> <p>The <code>_.chunk</code> function, by default, accepts an array and a number and splits and returns a new array representing the original array split into some number of arrays of the given size:</p> <pre><code class="lang-javascript">_.chunk([<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>], <span class="hljs-number">2</span>); <span class="hljs-comment">//=&gt; , [[0,1],[2,3]]</span> </code></pre> <p>If the original array cannot completely fulfill the chunk scheme then the array returned will drop the undersized final chunk:</p> <pre><code class="lang-javascript">_.chunk([<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>], <span class="hljs-number">2</span>); <span class="hljs-comment">//=&gt; , [[0,1],[2,3]]</span> </code></pre> <p>You can pass an optional third argument to <code>_.chunk</code> to pad out the final array chunk should it fall short. If the value given as the third argument is <em>not</em> an array then it is repeated the needed number of times:</p> <pre><code class="lang-javascript">_.chunk([<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>], <span class="hljs-number">3</span>, <span class="hljs-string">'_'</span>); <span class="hljs-comment">//=&gt; , [[0,1,2],[3,'_','_']]</span> </code></pre> <p>If you given an array as the optional third argument then that array is used to pad in-place as needed:</p> <pre><code class="lang-javascript">_.chunk([<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>], <span class="hljs-number">3</span>, [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>]); <span class="hljs-comment">//=&gt; , [[0,1,2],[3,'a','b']]</span> </code></pre> <hr> </div> </li> <li id="section-4"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-4">&#182;</a> </div> <h4 id="chunkall">chunkAll</h4> <p>The <code>_.chunkAll</code> function is similar to <code>_.chunk</code> except for the following. First, <code>_.chunkAll</code> will never drop short chunks from the end:</p> <pre><code class="lang-javascript">_.chunkAll([<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>], <span class="hljs-number">3</span>); <span class="hljs-comment">//=&gt; , [[0,1,2],[3]]</span> </code></pre> <p>Also, <code>_.chunkAll</code> takes an optional third argument signifying that paritions should be built from skipped regions:</p> <pre><code class="lang-javascript">_.chunkAll(_.range(<span class="hljs-number">1</span>), <span class="hljs-number">2</span>, <span class="hljs-number">4</span>); <span class="hljs-comment">//=&gt; [[0,1],[4,5],[8,9]]</span> </code></pre> <hr> </div> </li> <li id="section-5"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-5">&#182;</a> </div> <h4 id="cons">cons</h4> <p>Signature: <code>_.cons(head:Any, tail:Array)</code></p> <p>The <code>_.cons</code> function provides a way to “construct” a new array by taking some element and putting it at the front of another array.</p> <pre><code class="lang-javascript">_.cons(<span class="hljs-number">0</span>, []); <span class="hljs-comment">//=&gt; [0]</span> _.cons(<span class="hljs-number">1</span>, [<span class="hljs-number">2</span>]); <span class="hljs-comment">//=&gt; [1,2]</span> _.cons([<span class="hljs-number">0</span>], [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]); <span class="hljs-comment">//=&gt; [0,1,2,3]</span> </code></pre> <p>The <code>_.cons</code> function also can be used to create pairs if the second argument is not an array.</p> <p>Signature: <code>_.cons(head:Any, tail:Any)</code></p> <pre><code class="lang-javascript">_.cons(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <span class="hljs-comment">//=&gt; [1,2]</span> _.cons([<span class="hljs-number">1</span>], <span class="hljs-number">2</span>); <span class="hljs-comment">//=&gt; [[1],2]</span> </code></pre> <p>Finally, <code>_.cons</code> will operate with the <code>arguments</code> object.</p> <p>Signature: <code>_.cons(head:Any, tail:Arguments)</code></p> <pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-keyword">return</span> _.cons(<span class="hljs-number">0</span>, <span class="hljs-built_in">arguments</span>) } f(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>); <span class="hljs-comment">//=&gt; [0,1,2,3]</span> </code></pre> <h4 id="partition">partition</h4> <p>The <code>_.partition</code> function, by default, accepts an array and a number and splits and returns a new array representing the original array split into some number of arrays of the given size:</p> </div> <div class="content"><div class='highlight'><pre>_.partition([<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>], <span class="hljs-number">2</span>);</pre></div></div> </li> <li id="section-6"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>=&gt; , [[0,1],[2,3]]</p> </div> </li> <li id="section-7"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>If the original array cannot completely fulfill the partition scheme then the array returned will drop the undersized final partition:</p> </div> <div class="content"><div class='highlight'><pre>_.partition([<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>], <span class="hljs-number">2</span>);</pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>=&gt; , [[0,1],[2,3]]</p> </div> </li> <li id="section-9"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>You can pass an optional third argument to <code>_.partition</code> to pad out the final array partition should it fall short. If the value given as the third argument is <em>not</em> an array then it is repeated the needed number of times:</p> </div> <div class="content"><div class='highlight'><pre>_.partition([<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>], <span class="hljs-number">3</span>, <span class="hljs-string">'_'</span>);</pre></div></div> </li> <li id="section-10"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>=&gt; , [[0,1,2],[3,’<em>‘,’</em>‘]]</p> </div> </li> <li id="section-11"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>If you given an array as the optional third argument then that array is used to pad in-place as needed:</p> </div> <div class="content"><div class='highlight'><pre>_.partition([<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>], <span class="hljs-number">3</span>, [<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>]);</pre></div></div> </li> <li id="section-12"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>=&gt; , [[0,1,2],[3,’a’,’b’]]</p> </div> </li> <li id="section-13"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-13">&#182;</a> </div> <h4 id="partitionall">partitionAll</h4> <p>The <code>_.partitionAll</code> function is similar to <code>_.partition</code> except for the following. First, <code>_.partionAll</code> will never drop short partitions from the end:</p> </div> <div class="content"><div class='highlight'><pre>_.partitionAll([<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>], <span class="hljs-number">3</span>);</pre></div></div> </li> <li id="section-14"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>=&gt; , [[0,1,2],[3]]</p> </div> </li> <li id="section-15"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>Also, <code>_.paritionAll</code> takes an optional third argument signifying that paritions should be built from skipped regions:</p> </div> <div class="content"><div class='highlight'><pre>_.partitionAll(_.range(<span class="hljs-number">1</span>), <span class="hljs-number">2</span>, <span class="hljs-number">4</span>);</pre></div></div> </li> <li id="section-16"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>=&gt; [[0,1],[4,5],[8,9]]</p> </div> </li> <li id="section-17"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-17">&#182;</a> </div> <hr> </div> </li> <li id="section-18"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-18">&#182;</a> </div> <h4 id="cycle">cycle</h4> <p>The <code>_.cycle</code> function takes an integer value used to build an array of that size containing the number of iterations through the given array, strung end-to-end as many times as needed. An example is probably more instructive:</p> <pre><code class="lang-javascript">_.cycle(<span class="hljs-number">5</span>, [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]); <span class="hljs-comment">//=&gt; [1,2,3,1,2]</span> </code></pre> <hr> </div> </li> <li id="section-19"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-19">&#182;</a> </div> <h4 id="interpose">interpose</h4> <p>The <code>_.interpose</code> function takes an array and an element and returns a new array with the given element inserted betwixt every element in the original array:</p> <pre><code class="lang-javascript">_.interpose([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>], <span class="hljs-number">0</span>); <span class="hljs-comment">//=&gt; [1,0,2,0,3]</span> </code></pre> <p>If there are no betweens (i.e. empty and single-element arrays), then the original array is returned:</p> <pre><code class="lang-javascript">_.interpose([<span class="hljs-number">1</span>], <span class="hljs-number">0</span>); <span class="hljs-comment">//=&gt; [1]</span> _.interpose([], <span class="hljs-number">0</span>); <span class="hljs-comment">//=&gt; []</span> </code></pre> <hr> </div> </li> <li id="section-20"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-20">&#182;</a> </div> <h4 id="iterateuntil">iterateUntil</h4> <p>The <code>_.iterateUntil</code> function takes a function used as a result generator, a function used as a stop-check and a seed value and returns an array of each generated result. The operation of <code>_.iterateUntil</code> is such that the result generator is passed the seed to start and each subsequent result which will continue <strong>until</strong> a result fails the check function (i.e. returns falsey). An example is best:</p> <pre><code class="lang-javascript"><span class="hljs-keyword">var</span> dec = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">n</span>) </span>{ <span class="hljs-keyword">return</span> n - <span class="hljs-number">1</span>; }; <span class="hljs-keyword">var</span> isPos = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">n</span>) </span>{ <span class="hljs-keyword">return</span> n &gt; <span class="hljs-number">0</span>; }; </code></pre> <p>The <code>dec</code> result generator takes a number and decrements it by one. The <code>isPos</code> predicate takes a number and returns <code>true</code> if it’s positive. Using these two functions you can build an array of every number from <code>6</code> to <code>0</code>, inclusive:</p> <pre><code class="lang-javascript">_.iterateUntil(dec, isPos, <span class="hljs-number">6</span>); <span class="hljs-comment">//=&gt; [5,4,3,2,1]</span> </code></pre> <p>That is, the array only contains every number from <code>5</code> down to <code>1</code> because when the result of <code>dec</code> got to <code>0</code> the <code>isPos</code> check failed (i.e. went falsey) thus terminating the execution.</p> <hr> </div> </li> <li id="section-21"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-21">&#182;</a> </div> <h4 id="keepindexed">keepIndexed</h4> <p>The <code>_.keepIndexed</code> function takes an array and a function and returns a new array filled with the <em>non-null</em> return results of the given function on the elements or keys in the given array:</p> <pre><code class="lang-javascript">_.keepIndexed([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>], <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">k</span>) </span>{ <span class="hljs-keyword">return</span> i === <span class="hljs-number">1</span> || i === <span class="hljs-number">2</span>; }); <span class="hljs-comment">//=&gt; [false, true, true]</span> </code></pre> <p>If you return either <code>null</code> or <code>undefined</code> then the result is dropped from the resulting array:</p> <pre><code class="lang-javascript">_.keepIndexed([<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>,<span class="hljs-string">'c'</span>], <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">k, v</span>) </span>{ <span class="hljs-keyword">if</span> (k === <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> v; }); <span class="hljs-comment">//=&gt; ['b']</span> </code></pre> <hr> </div> </li> <li id="section-22"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-22">&#182;</a> </div> <h4 id="mapcat">mapcat</h4> <p>There are times when a mapping operation produces results contained in arrays, but the final result should be flattened one level. For these circumstances you can use <code>_.mapcat</code> to produce results:</p> <pre><code class="lang-javascript"><span class="hljs-keyword">var</span> array = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-literal">null</span>,<span class="hljs-number">4</span>,<span class="hljs-literal">undefined</span>,<span class="hljs-number">6</span>]; <span class="hljs-keyword">var</span> errors = _.mapcat(array, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e,i</span>) </span>{ <span class="hljs-keyword">if</span> (e == <span class="hljs-literal">null</span>) { <span class="hljs-keyword">return</span> [<span class="hljs-string">"Element @"</span> + i + <span class="hljs-string">" is bad"</span>]; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> []; } }); </code></pre> <p>Inspecting the contents of <code>errors</code> shows:</p> <pre><code class="lang-javascript">[<span class="hljs-string">"Element @2 is bad"</span>, <span class="hljs-string">"Element @4 is bad"</span>] </code></pre> <p>The <code>_.mapcat</code> function is equivalent to <code>_.cat.apply(array, _.map(array,fun))</code>.</p> <hr> </div> </li> <li id="section-23"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-23">&#182;</a> </div> <h4 id="reductions">reductions</h4> <p>The <code>_.reductions</code> function is similar to Underscore’s builtin <code>_.reduce</code> function except that it returns an array of every intermediate value in the folding operation:</p> <pre><code class="lang-javascript">_.reductions([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>], <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">agg, n</span>) </span>{ <span class="hljs-keyword">return</span> agg + n; }, <span class="hljs-number">0</span>); <span class="hljs-comment">//=&gt; [1,3,6,10,15]</span> </code></pre> <p>The last element in the array returned from <code>_.reductions</code> is the answer that you would get if you had just chosen to use <code>_.reduce</code>.</p> <hr> </div> </li> <li id="section-24"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-24">&#182;</a> </div> <h4 id="repeat">repeat</h4> <p>Signature: <code>_.repeat(t:Integer, value:Any)</code></p> <p>The <code>_.repeat</code> function takes an integer value used to build an array of that size containing the value given:</p> <pre><code class="lang-javascript">_.repeat(<span class="hljs-number">5</span>, <span class="hljs-string">'a'</span>); <span class="hljs-comment">//=&gt; ['a','a','a','a','a']</span> </code></pre> <hr> </div> </li> <li id="section-25"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-25">&#182;</a> </div> </div> </li> <li id="section-26"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-26">&#182;</a> </div> <h4 id="splitat">splitAt</h4> <p>The <code>_.splitAt</code> function takes an array and a numeric index and returns a new array with two embedded arrays representing a split of the original array at the index provided:</p> <pre><code class="lang-javascript">_.splitAt([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>], <span class="hljs-number">2</span>); <span class="hljs-comment">//=&gt; [[1,2],[3,4,5]]</span> _.splitAt([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>], <span class="hljs-number">0</span>); <span class="hljs-comment">//=&gt; [[],[1,2,3,4,5]]</span> </code></pre> <p>The operation of <code>_.splitAt</code> is safe if the index provided is outside the range of legal indices:</p> <pre><code class="lang-javascript">_.splitAt([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>], <span class="hljs-number">20000</span>); <span class="hljs-comment">//=&gt; [[1,2,3,4,5],[]]</span> _.splitAt([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>], -<span class="hljs-number">1000</span>); <span class="hljs-comment">//=&gt; [[],[1,2,3,4,5]] </span> _.splitAt([], <span class="hljs-number">0</span>); <span class="hljs-comment">//=&gt; [[],[]]</span> </code></pre> <hr> </div> </li> <li id="section-27"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-27">&#182;</a> </div> <h4 id="takeskipping">takeSkipping</h4> <p>The <code>_.takeSkipping</code> function takes an array and a number and returns a new array containing every nth element in the original array:</p> <pre><code class="lang-javascript">_.takeSkipping(_.range(<span class="hljs-number">10</span>), <span class="hljs-number">2</span>); <span class="hljs-comment">//=&gt; [0,2,4,6,8]</span> </code></pre> <p>The <code>_.takeSkipping</code> function is safe against numbers larger or smaller than the array size:</p> <pre><code class="lang-javascript">_.takeSkipping(_.range(<span class="hljs-number">10</span>), <span class="hljs-number">100000</span>); <span class="hljs-comment">//=&gt; [0]</span> _.takeSkipping(_.range(<span class="hljs-number">10</span>), -<span class="hljs-number">100</span>); <span class="hljs-comment">//=&gt; []</span> </code></pre> <hr> </div> </li> <li id="section-28"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-28">&#182;</a> </div> <h4 id="weave">weave</h4> <p>The <code>_.weave</code> function works similarly to <code>_.interpose</code> (shown above) except that it accepts an array used as the interposition values. In other words, <code>_.weave</code> takes two arrays and returns a new array with the original elements woven together. An example would help:</p> <pre><code class="lang-javascript">_.weave([<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>,<span class="hljs-string">'c'</span>], [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]); <span class="hljs-comment">//=&gt; ['a',1,'b',2,'c',3]</span> </code></pre> <p>The array returned from <code>_.weave</code> will be as long as the longest array given with the woven entries stopping according to the shortest array:</p> <pre><code class="lang-javascript">_.weave([<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>,<span class="hljs-string">'c'</span>], [<span class="hljs-number">1</span>]); <span class="hljs-comment">//=&gt; ['a',1,'b','c']</span> </code></pre> <p>The <code>_.interleave</code> function is an alias for <code>_.weave</code>.</p> <hr> </div> </li> <li id="section-29"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-29">&#182;</a> </div> </div> </li> </ul> </div> </body> </html>