mozilla

Revision 29725 of Array.prototype.some()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/some
  • Revision title: Array some method
  • Revision id: 29725
  • Created:
  • Creator: Waldo
  • Is current revision? No
  • Comment some treats the given array as sparse -- for example, (a=[],a[5]=5,a).some(function(v){if(v!=5)return false; return true;}) === true

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: none

Syntax

some(callback{{mediawiki.external(', <i>thisObject</i>')}})

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.

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);
}
passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 44].some(isBigEnough);
// passed is true
{{ wiki.languages( { "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/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>none</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
some(<i>callback</i>{{mediawiki.external(', &lt;i&gt;thisObject&lt;/i&gt;')}})
</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="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>function isBigEnough(element, index, array) {
  return (element &gt;= 10);
}
passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 44].some(isBigEnough);
// passed is true
</pre>
{{ wiki.languages( { "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Array/some" } ) }}
Revert to this revision