Open In App
Related Articles

Underscore.js _.without() Function

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

Underscore.js _.without() function is used to return a copy of the array which contains all the array except values.

Syntax:

_.without( array, *values );

Parameters:

  • array: This parameter is used to hold the list of array elements.
  • values: This parameter is used to hold the value that needs to be removed from the list of arrays.

Return value:

It returns a copy of the array without the mentioned elements of the passed array.

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

The ._without() function takes the element from the list one by one and checks whether it is the unnecessary element mentioned in the second parameter or not. If it is, then it is not included in the resultant array otherwise it is included.

Example: This example shows passing a list of numbers to _.without() function.

html

<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.without([0, 1, 2, 3, 0, 1, 3, 4], 0, 1));
    </script>
</body>
 
</html>

                    

Output:

Passing the false elements to _.without() function:

The ._without() function responds similarly by taking the element from the list one by one and checks whether it is the unnecessary element mentioned in the second parameter or not. If it is, then it is not included in the resultant array otherwise it is included. It does not bother whether it is a true element or not. This implies the_.without() function takes all the elements equally while processing.

Example: This example shows passing the false element to _.without() function.

html

<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.without([0, 1, 2, 4, undefined, null,
            3, 1, 4, 0, "", ''], 0, null, '')
        );
    </script>
</body>
 
</html>

                    

Output:

Passing the case sensitive element (upper/lower case) to _.without() function:

The ._without() function will works the same. In this function passing the element which is present in the given array in case sensitive (uppercase) format. It is not excluded from the resultant array which means that the _.without() function is case sensitive.

Example: This example shows passing the case sensitive element (upper/lower case) to _.without() function.

html

<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.without(["HTML", "CSS", "JS", "AJAX"], "ajax"));
    </script>
</body>
 
</html>

                    

Output:

Passing the element in the same case to the _.without() function:

Passing the second parameter as mentioned in the passed array then the element (“AJAX” here) gets excluded.

Example: This example shows passing the element in the same case to _.without() function.

html

<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.without(["HTML", "CSS", "JS", "AJAX"], "AJAX"));
    </script>
</body>
 
</html>

                    

Output:

Note: These commands will not work in Google console or in Firefox as for these additional files need to be added which they didn’t have added. So, add the given links to your HTML file and then run them.

<script type="text/javascript" src = 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>


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