Array.prototype.some()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/some
  • Revision title: Array some method
  • Revision id: 29744
  • Created:
  • Creator: jdalton
  • Is current revision? No
  • Comment Cleanup compat shim.; 2 words added, 7 words removed

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
ECMAScript Edition ECMAScript 5th Edition

Syntax

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 recent addition 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 implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming Object and TypeError have their original values and that fun.call evaluates to the original value of Function.prototype.call.  {{ var DO_NOT_MODIFY_THE_FOLLOWING_CODE_IN_ANY_WAY = 0; }}

 

<meta http-equiv="content-type" content="text/html; charset=utf-8"/> <meta http-equiv="content-type" content="text/html; charset=utf-8"/>

if (!Array.prototype.some)
{
  Array.prototype.some = function(fun /*, thisp */)
  {
    "use strict";

    if (this == null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in t && fun.call(thisp, t[i], i, t))
        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

Browser compatibility

Based on Kangax's compat tables

{{ CompatibilityTable() }}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} 9 {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

{{ 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="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Array" title="en/JavaScript/Reference/Global_Objects/Array"><code>Array</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.6</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 5th Edition</td> </tr> </tbody>
</table>
<h3 name="Syntax">Syntax</h3>
<p><code><em>array</em>.some(<em>callback</em>[, <em>thisObject</em>])</code></p>
<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 recent addition 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 implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming <code style="color: inherit; font-weight: inherit; ">Object </code>and <code style="color: inherit; font-weight: inherit; ">TypeError</code> have their original values and that <code style="color: inherit; font-weight: inherit; ">fun.call</code> evaluates to the original value of <code style="color: inherit; font-weight: inherit; "><a href="/en/JavaScript/Reference/Global_Objects/Function/call" rel="internal" style="text-decoration: none; color: rgb(4, 121, 167); ">Function.prototype.call</a></code>.  {{ var DO_NOT_MODIFY_THE_FOLLOWING_CODE_IN_ANY_WAY = 0; }}</p>
<p> </p>
<p>&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"/&gt; &lt;meta http-equiv="content-type" content="text/html; charset=utf-8"/&gt;</p>
<pre class="eval">if (!Array.prototype.some)
{
  Array.prototype.some = function(fun /*, thisp */)
  {
    "use strict";

    if (this == null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length &gt;&gt;&gt; 0;
    if (typeof fun != "function")
      throw new TypeError();

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

    return false;
  };
}
</pre>
<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>
<h3>Browser compatibility</h3>
<p>Based on <a class="external" href="http://kangax.github.com/es5-compat-table/">Kangax's compat tables</a></p>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox (Gecko)</th> <th>Chrome</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> <td>9</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox Mobile (Gecko)</th> <th>Android</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> </tbody> </table>
</div>
<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