_.compact( list )
Parameters: This function contains single parameter list which holds the array containing the true and false elements.
Return value: It returns an array containing only true values.
Passing a list of both the true and the false elements to _.compact() function: The _.compact() function start by taking the elements one by one and then checks whether it is a false element or not. If it is false element then it just ignores that element. Otherwise it adds the true element to the resultant array. Here the false elements represented as false and an empty string represented by ”.
Passing a list containing all the false values to the _.comapct() function: If passed array contains all the false elements then the _.compact() function will work the same. It will check each element and since they are all false so all the elements will be ignored. So, the resultant array formed will not have any element and it’s length will be 0.
Passing a list which contains a false element in ” to _.compact() function: Pass a false element, undefined inside ” as ‘undefined’. Though this is a false element but since it is given inside ” therefore it is treated as a character element. Hence, it is no longer a false element. Rest it works the same as above.
Passing a list containing modified false values to the _.reduce() function: The array contains an element as true which is included in the resultant array. The ‘no’ element is also included as it is inside ” which makes it a character. Also if pass ‘no2’ it is also not ignored by the _.compact() function.
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.
- PHP | compact() Function
- HTML | <ol> compact Attribute
- HTML | <dir> compact Attribute
- Underscore.js | min() with Examples
- Underscore.js | max() with Examples
- jQuery | eq() with Examples
- jQuery | first() with Examples
- Underscore.js | _.last() with Examples
- Underscore.js | map() with Examples
- jQuery | last() with Examples
- jQuery | one() with Examples
- Underscore.js | where() with Examples
- jQuery | on() with Examples
- jQuery | after() with Examples
- Underscore.js | first() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.