lodash-contrib
Version:
The brass buckles on lodash's utility belt
334 lines (243 loc) • 14.2 kB
HTML
<html>
<head>
<title>_.array.selectors.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 …</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.selectors.js.md</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<h3 id="array-selectors">array.selectors</h3>
<blockquote>
<p>Functions to take things from arrays. <a href="docs/lodash.array.selectors.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">¶</a>
</div>
<h4 id="best">best</h4>
<p><strong>Signature:</strong> <code>_.best(array:Array, fun:Function)</code></p>
<p>Returns the “best” value in an array based on the result of a given function.</p>
<pre><code class="lang-javascript">_.best([<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">x, y</span>) </span>{
<span class="hljs-keyword">return</span> x > y;
});
<span class="hljs-comment">//=> 5</span>
</code></pre>
<hr>
</div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<h4 id="dropwhile">dropWhile</h4>
<p><strong>Signature:</strong> <code>_.dropWhile(array:Array, pred:Function)</code></p>
<p>Drops elements for which the given function returns a truthy value.</p>
<pre><code class="lang-javascript">_.dropWhile([-<span class="hljs-number">2</span>,-<span class="hljs-number">1</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>], isNeg);
<span class="hljs-comment">//=> [0,1,2]</span>
</code></pre>
<hr>
</div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<h4 id="keep">keep</h4>
<p><strong>Signature:</strong> <code>_.keep(array:Array, fun:Function)</code></p>
<p>Returns an array of existy results of a function over a source array.</p>
<pre><code class="lang-javascript">_.keep([<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">val</span>) </span>{
<span class="hljs-keyword">if</span> (val % <span class="hljs-number">3</span> === <span class="hljs-number">0</span>) {
<span class="hljs-keyword">return</span> val;
}
});
<span class="hljs-comment">// => [3]</span>
</code></pre>
<hr>
</div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<h4 id="nth">nth</h4>
<p><strong>Signature:</strong> <code>_.nth(array:Array, index:Number[, guard:Any])</code></p>
<p>The <code>_.nth</code> function is a convenience for the equivalent <code>array[n]</code>. The
optional <code>guard</code> value allows <code>_.nth</code> to work correctly as a callback for
<code>_.map</code>.</p>
<pre><code class="lang-javascript">_.nth([<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>,<span class="hljs-string">'c'</span>], <span class="hljs-number">2</span>);
<span class="hljs-comment">//=> 'c'</span>
</code></pre>
<p>If given an index out of bounds then <code>_.nth</code> will return <code>undefined</code>:</p>
<pre><code class="lang-javascript">_.nth([<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>,<span class="hljs-string">'c'</span>], <span class="hljs-number">2000</span>);
<span class="hljs-comment">//=> undefined</span>
</code></pre>
<p>The <code>_.nth</code> function can also be used in conjunction with <code>_.map</code> and <code>_.compact</code> like so:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> b = [[<span class="hljs-string">'a'</span>],[<span class="hljs-string">'b'</span>],[]];
_.compact(_.map(b, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) </span>{ <span class="hljs-keyword">return</span> _.nth(e,<span class="hljs-number">0</span>) }));
<span class="hljs-comment">//=> ['a','b']</span>
</code></pre>
<p>If wrapping a function around <code>_.nth</code> is too tedious or you’d like to partially apply the index then Underscore-contrib offers any of <code>_.flip2</code>, <code>_.fix</code> or <code>_.curryRight2</code> to solve this.</p>
<hr>
</div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<h4 id="partitionby">partitionBy</h4>
<p><strong>Signature:</strong> <code>_.keep(array:Array, fun:Function)</code></p>
<p>Takes an array and partitions it into sub-arrays as the given predicate changes
truth sense.</p>
<pre><code class="lang-javascript">_.partitionBy([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">5</span>], _.isEven);
<span class="hljs-comment">// => [[1],[2,2],[3,1,1,5]]</span>
_.partitionBy([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">5</span>], _.identity);
<span class="hljs-comment">// => [[1],[2,2],[3],[1,1],[5]]</span>
</code></pre>
<hr>
</div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<h4 id="second">second</h4>
<p><strong>Signature:</strong> <code>_.second(array:Array, index:Number[, guard:Any])</code></p>
<p>The <code>_.second</code> function is a convenience for the equivalent <code>array[1]</code>. The
optional <code>guard</code> value allows <code>_.second</code> to work correctly as a callback for
<code>_.map</code>.</p>
<pre><code class="lang-javascript">_.second([<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>]);
<span class="hljs-comment">//=> 'b'</span>
_.map([[<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>], _.range(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>)], _.second);
<span class="hljs-comment">//=> ['b',11]</span>
</code></pre>
<p>You can also pass an optional number to the <code>_.second</code> function to take a number of elements from an array starting with the second element and ending at the given index:</p>
<pre><code class="lang-javascript">_.second(_.range(<span class="hljs-number">10</span>), <span class="hljs-number">5</span>)
<span class="hljs-comment">//=> [1, 2, 3, 4]</span>
</code></pre>
<hr>
</div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<h4 id="takewhile">takeWhile</h4>
<p><strong>Signature:</strong> <code>_.takeWhile(array:Array, pred:Function)</code></p>
<p>The <code>_.takeWhile</code> function takes an array and a function and returns a new array containing the first n elements in the original array for which the given function returns a truthy value:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> isNeg = <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">0</span>; };
_.takeWhile([-<span class="hljs-number">2</span>,-<span class="hljs-number">1</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>], isNeg);
<span class="hljs-comment">//=> [-2,-1]</span>
</code></pre>
<hr>
</div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<h4 id="third">third</h4>
<p><strong>Signature:</strong> <code>_.third(array:Array, index:Number[, guard:Any])</code></p>
<p>The <code>_.third</code> function is a convenience for the equivalent <code>array[2]</code>. The
optional <code>guard</code> value allows <code>_.third</code> to work correctly as a callback for
<code>_.map</code>.</p>
<pre><code class="lang-javascript">_.third([<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>,<span class="hljs-string">'c'</span>]);
<span class="hljs-comment">//=> 'c'</span>
_.map([[<span class="hljs-string">'a'</span>,<span class="hljs-string">'b'</span>,<span class="hljs-string">'c'</span>], _.range(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>)], _.third);
<span class="hljs-comment">//=> ['c',12]</span>
</code></pre>
<p>You can also pass an optional number to the <code>_.third</code> function to take a number of elements from an array starting with the third element and ending at the given index:</p>
<pre><code class="lang-javascript">_.third(_.range(<span class="hljs-number">10</span>), <span class="hljs-number">5</span>)
<span class="hljs-comment">//=> [2, 3, 4]</span>
</code></pre>
<hr>
</div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
</div>
</li>
</ul>
</div>
</body>
</html>