Array some()

JavaScript Array some() Definition

The some () method scans each element in the array, respectively. Ends scanning when the desired condition is met. Returns true. Returns false if the condition is not satisfied until the last element.

Syntax:

thisArray.some(function(currentValue, index, arr), thisValue)

currentValue (required): The value of the element.
index (optional): The array index of the element.
arr (optional): The array to which the element belongs.

Let’s try to understand the logic with a small example.

        let numbers = [2, 5, 7, 9];

        function controlNumbers(num) {
            return num > 5;
        }

        console.log(numbers.some(controlNumbers));

true

a value within the directory to provide the condition is sufficient. Returns false if no array element provides the condition.

        let numbers = [ 2, 5, 3, 1 ];

        function controlNumbers( num ) {
            return num > 5;
        }

        console.log(numbers.some(controlNumbers));

false


JavaScript Array some() Example

Example 1

        let salary = [1500, 3000, 4000, 4500];

        function badSalary( money ) {
            return money < 3000;
        }

        console.log("bad salary: " + salary.some( badSalary ));

        function goodSalary( money ) {
            return money > 5000;
        }

        console.log("good salary: " + salary.some( goodSalary ));

output:

bad salary: true
good salary: false


Example 2

        let arr1 = [10, 20, 30].some( function (a) { return a > 20 });
        let arr2 = [10, 20, 30].some( function (a) { return a > 40 });
        console.log(arr1);
        console.log(arr2);

output:

true
false


Example 3

Javascript array some
        function some_function(element, index, array) {
            if (element.length > 5)
                return true;
            else
                return false;
        }
        function control() {
            var list = ["john", "rick", "alex", "oliver"];
            if (list.some(some_function)) {
                alert("There's someone with more than five letters.")
            } else {
                alert("There's not someone with more than five letters.")
            }
        }
        control();

Example 4

        let carList = ["bmw", "audi", "toyota", "opel"];

        function controlCar(x, y) {
            return x.some(function ( listValue ) {
                return y == listValue;
            })
        }

        console.log("bmw: " + controlCar( carList, "bmw"));
        console.log("porsche: " + controlCar( carList, "porsche"));
        console.log("audi: " + controlCar( carList, "audi"));
        console.log("mercedes: " + controlCar( carList, "mercedes"));
        console.log("toyota: " + controlCar( carList, "toyota"));
        console.log("range rover: " + controlCar( carList, "renge rover"));
        console.log("opel: " + controlCar( carList, "opel"));

output:

bmw: true
porsche: false
audi: true
mercedes: false
toyota: true
range rover: false
opel: true


Polyfill

if ( !Array.prototype.some) {
  Array.prototype.some = function( fun, thisArg) {
    'use strict';

    if (this == null) {
      throw new TypeError( 'Array.prototype.some called on null or undefined');
    }

    if (typeof fun !== 'function') {
      throw new TypeError();
    }

    var t = Object( this);
    var len = t.length >>> 0;

    for (var i = 0; i < len; i++) {
      if (i in t && fun.call(thisArg, t[i], i, t)) {
        return true;
      }
    }

    return false;
  };
}

Browser Support

Chromeyes
Edgeyes
Firefox1.5
Internet Explorer9
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

2 thoughts on “Array some()

Add yours

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started