Export (0) Print
Expand All

some Method (JavaScript)

Checks whether a defined callback function returns true for any element of an array.

array1.some(callbackfn[, thisArg])

Parameter

Definition

array1

Required. An array object.

callbackfn

Required. A function that accepts up to three arguments. The some method calls the callbackfn function for each element in array1 until the callbackfn returns true, or until the end of the array.

thisArg

Optional. An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

true if the callbackfn function returns true for any array element; otherwise, false.

If the callbackfn argument is not a function object, a TypeError exception is thrown.

The some method calls the callbackfn function one time for each array element, in ascending index order, until the callbackfn function returns true. If an element that causes callbackfn to return true is found, the some method immediately returns true. Otherwise, the some method returns false.

The callback function is not called for missing elements of the array.

In addition to array objects, the some method can be used by any object that has a length property and that has numerically indexed property names.

NoteNote

You can use the every Method (JavaScript) to check whether the callback function returns true for all elements of an array.

Callback Function Syntax

The syntax of the callback function is as follows:

function callbackfn(value, index, array1)

The callback function is always called with three arguments. You can declare the callback function with up to three parameters.

The following table lists the callback function parameters.

Callback parameter

Definition

value

The value of the array element.

index

The numeric index of the array element.

array1

The array object that contains the element.

Modifying the Array Object

The array object can be modified by the callback function.

The following table describes the results of modifying the array object after the some method starts.

Condition after the some method starts

Element passed to callback function?

Element is added beyond the original length of the array.

No.

Element is added to fill in a missing element of the array.

Yes, if that index has not yet been passed to the callback function.

Element is changed.

Yes, if that element has not yet been passed to the callback function.

Element is deleted from the array.

No, unless that element has already been passed to the callback function.

The following example illustrates the use of the some method.

// Define the callback function.
function CheckIfEven(value, index, ar) {
    document.write(value + " ");

    if (value % 2 == 0)
        return true;
    else
        return false;
}

// Create an array.
var numbers = [1, 15, 4, 10, 11];

// Check whether the callback function returns true for any of the
// array values.
if (numbers.some(CheckIfEven))
    document.write("Found");
else
    document.write("Not Found");

// Output:
//  1 15 4 Found

The following example illustrates the use of the thisArg argument, which specifies an object to which the this keyword can refer.

// Create a function that returns true if the value is outside
// the range or non-numeric.
var checkOutsideNumericRange = function (value) {
    if (typeof value !== 'number')
        return true;
    else
        return value < this.minimum || value > this.maximum;
}

// Create an array of numbers and strings.
var numbers = [6, 12, "15", 16, "the", -12];

// Check whether the callback function returns true for
// any of the array values.
// The obj argument enables use of the this value
// within the callback function.

var obj = { minimum: 10, maximum: 20 }

if (numbers.some(checkOutsideNumericRange, obj)) {
    // Create an array that contains the values outside the range.
    var result = numbers.filter(checkOutsideNumericRange, obj);
    document.write(result);
}
else {
    document.write("None are within range.");
}

// Output: 6,15,the,-12

Supported in the Internet Explorer 9 standards document mode. See Version Information.

Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards.

Community Additions

Show:
© 2014 Microsoft