Open In App

Underscore.js _.some Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Underscore.js _.some() function is used to find whether any value in the given list matches the given condition or not. If at least one value satisfies this condition then the output will be true. When none of the values matches then the output will be false. 

Syntax: 

_.some(list, [predicate], [context]);

Parameters:

  • List: This parameter contains the list of data.
  • Predicate: This parameter is used to hold the test condition.
  • Context: This parameter contains the text that needs to be displayed.

Return values:

The return value that is either trueThe either true return value (when at least one element of the list fulfills the given condition) or false (when none of the elements fulfill the condition).

Passing an array to the _.some() function():

The ._some() function takes the elements from the list one by one and then checks the condition by doing the specified operations on the code. The operation is to find whether the array contains any true elements or not.

Example: This example shows passing an array to the _.some() function.

HTML




<html>
 
<head>
    <title>_.some() function</title>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.some([null, 0, 1, false]));
    </script>
</body>
 
</html>


Output: 

Example: In the below code since the array contains all false elements like ‘0’, ‘false’, ‘null’ and no true elements, so the output will be ‘false’.

HTML




<html>
 
<head>
    <title>_.some() function</title>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.some([null, 0, false]));
    </script>
</body>
 
</html>


Output: 

Passing a list of numbers and a function to _.some() function:

First, define both list and the function which need to performed/checked on the list. Then pass the list and the function as arguments to the _.some() function. If the condition given in the function is satisfied even by 1 of the element of the list then the output will be true.

Example: This example shows passing a list of numbers and a function _.some() function.

HTML




<!-- Write HTML code here -->
<html>
 
<head>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        let isEven = function (value) {
            return value % 2 === 0;
        };
        console.log(_.some(values, isEven));
    </script>
</body>
 
</html>


Output: 

Passing a structure to the _.some() function:

First declare the array (here array is ‘people’). Choose one condition on which need to check like here ‘longHairs’. Console.log the final answer. Since, three people’s ‘longHair’ property is true so the resultant is also true.

Example: This example shows passing a structure to the _.some() function.

HTML




<html>
 
<head>
    <title>_.some() function</title>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let people = [
            { name: 'sakshi', LongHair: '' },
            { name: 'aishwarya', LongHair: true },
            { name: 'akansha', LongHair: true },
            { name: 'preeti', LongHair: true }
        ],
 
            hasLongHairs = function (value) {
                return (value.LongHair !== '');
            };
 
        console.log(_.some(people, hasLongHairs));
    </script>
</body>
 
</html>


Output: 

Using two _.some() function together:

Passing different objects to each _.some() function and then use the following results together by using the logical operators like ‘&&’, ‘||’, ‘!’ etc. The object1 and arralist1 contains atleast one true value so the resultant of two true will also be true. Hence, first condition is satisfied. The object2 is empty and arraylist2 also is also empty so they are not valid. Since use ‘!’ before every _.some() function so the resultant are 2 true values.

Example: This example shows use of the _.some() function and another custom.

HTML




<html>
 
<head>
    <title>_.some() function</title>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let listOne = [null, , undefined, {}, 0];
        let listTwo = [];
        let objectOne = {
            property1: null,
            property3: true
        };
        let objectTwo = {};
        if (_.some(listOne) && _.some(objectOne)) {
            console.log("atleast one is valid\n")
        };
        if (!_.some(listTwo) && !_.some(objectTwo)) {
            console.log("not valid\n")
        };
    </script>
</body>
 
</html>


Output: 
 

Example: This example shows use of the _.some() function.

HTML




<html>
 
<head>
    <title>_.some() function</title>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let values = [1, 3, 5, 7, 9];
        let isEven = function (value) {
            return value % 2 === 0;
        };
        console.log(_.some(values, isEven));
    </script>
</body>
 
</html>


Output: 

 



Last Updated : 13 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads