Underscore.js _.without() Function
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:
<!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:
<!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:
<!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:
<!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 = </ script > |
Please Login to comment...