Underscore.JS - compact method



Syntax

_.compact(list)

compact method removes falsy values(false, null, 0, "", NaN and undefined) from the given list.

Example

var _ = require('underscore');

//Example: compact a list
result = _.compact([0, 1, false, 2, '', 3, null, NaN, undefined]);
console.log(result)

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

[ 1, 2, 3 ]
underscorejs_processing_collection.htm
Advertisements