Array.prototype.some()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/some
  • Revision title: Array some method
  • Revision id: 29735
  • Created:
  • Creator: Inimino
  • Is current revision? No
  • Comment 5 words added, 1 words removed; page display name reset to default

Revision Content

 

Summary

Tests whether some element in the array passes the test implemented by the provided function.

Method of Array
Implemented in: JavaScript 1.6 (Gecko 1.8b2 and later)
ECMAScript Edition: ECMA-262 Edition 5

Syntax

var someElementPassed = array.some(callback[, thisObject]);

Parameters

callback 
Function to test for each element.
thisObject 
Object to use as this when executing callback.

Description

some executes the callback function once for each element present in the array until it finds one where callback returns a true value. If such an element is found, some immediately returns true. Otherwise, some returns false. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.

If a thisObject parameter is provided to some, it will be used as the this for each invocation of the callback. If it is not provided, or is null, the global object associated with callback is used instead.

some does not mutate the array on which it is called.

The range of elements processed by some is set before the first invocation of callback. Elements that are appended to the array after the call to some begins will not be visited by callback. If an existing, unvisited element of the array is changed by callback, its value passed to the visiting callback will be the value at the time that some visits that element's index; elements that are deleted are not visited.

Compatibility

some is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of some in ECMA-262 implementations which do not natively support it.  This algorithm is exactly the one used in Firefox and SpiderMonkey.

if (!Array.prototype.some)
{
  Array.prototype.some = function(fun /*, thisp*/)
  {
    var i = 0,
        len = this.length >>> 0;

    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (; i < len; i++)
    {
      if (i in this &&
          fun.call(thisp, this[i], i, this))
        return true;
    }

    return false;
  };
}

 

Examples

Example: Testing size of all array elements

The following example tests whether some element in the array is bigger than 10.

function isBigEnough(element, index, array) {
  return (element >= 10);
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough);
// passed is true

 

{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Array/some", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Array/some", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Array/some" } ) }}

Revision Source

<p> </p>
<h3 name="Summary">Summary</h3>
<p>Tests whether some element in the array passes the test implemented by the provided function.</p>
<table class="fullwidth-table"> <tbody> <tr> <td class="header" colspan="2">Method of <a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/Array" title="en/Core_JavaScript_1.5_Reference/Objects/Array">Array</a></td> </tr> <tr> <td>Implemented in:</td> <td>JavaScript 1.6 (Gecko 1.8b2 and later)</td> </tr> <tr> <td>ECMAScript Edition:</td> <td>ECMA-262 Edition 5</td> </tr> </tbody>
</table>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">var <em>someElementPassed</em> = <em>array</em>.some(<em>callback</em>[, <em>thisObject</em>]);
</pre>
<h3 name="Parameters">Parameters</h3>
<dl> <dt><code>callback</code> </dt> <dd>Function to test for each element.</dd> <dt><code>thisObject</code> </dt> <dd>Object to use as <code>this</code> when executing <code>callback</code>.</dd>
</dl>
<h3 name="Description">Description</h3>
<p><code>some</code> executes the <code>callback</code> function once for each element present in the array until it finds one where <code>callback</code> returns a true value. If such an element is found, <code>some</code> immediately returns <code>true</code>. Otherwise, <code>some</code> returns <code>false</code>. <code>callback</code> is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.</p>
<p><code>callback</code> is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.</p>
<p>If a <code>thisObject</code> parameter is provided to <code>some</code>, it will be used as the <code>this</code> for each invocation of the <code>callback</code>. If it is not provided, or is <code>null</code>, the global object associated with <code>callback</code> is used instead.</p>
<p><code>some</code> does not mutate the array on which it is called.</p>
<p>The range of elements processed by <code>some</code> is set before the first invocation of <code>callback</code>. Elements that are appended to the array after the call to <code>some</code> begins will not be visited by <code>callback</code>. If an existing, unvisited element of the array is changed by <code>callback</code>, its value passed to the visiting <code>callback</code> will be the value at the time that <code>some</code> visits that element's index; elements that are deleted are not visited.</p>
<h3 name="Compatibility">Compatibility</h3>
<p><code>some</code> is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of <code>some</code> in ECMA-262 implementations which do not natively support it.  This algorithm is exactly the one used in Firefox and SpiderMonkey.</p>
<pre class="eval">if (!Array.prototype.some)
{
  Array.prototype.some = function(fun /*, thisp*/)
  {
    var i = 0,
        len = this.length &gt;&gt;&gt; 0;

    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (; i &lt; len; i++)
    {
      if (i in this &amp;&amp;
          fun.call(thisp, this[i], i, this))
        return true;
    }

    return false;
  };
}
</pre>
<p> </p>
<h3 name="Examples">Examples</h3>
<h4 name="Example:_Testing_size_of_all_array_elements">Example: Testing size of all array elements</h4>
<p>The following example tests whether some element in the array is bigger than 10.</p>
<pre class="eval">function isBigEnough(element, index, array) {
  return (element &gt;= 10);
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough);
// passed is true
</pre>
<p> </p>
<p>{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Array/some", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Array/some", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Array/some" } ) }}</p>
Revert to this revision