Underscore.js | _.without() with Examples

The Underscore.js is a JavaScript library that provides a lot of useful functions like the map, filter, invoke etc even without using any built-in objects.
The _.without() function is used to return a copy of array which contains all the array except values.

Syntax:

_.without( array, *values )

Parameters: This function accepts two parameter which are listed below:



  • array: This parameter is used to hold the list of array elements.
  • values: This parameter is used to hold the value which need to remove from the list of array.

Return value: It return a copy of array without 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript" src
</script

chevron_right




My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.