- Underscore.JS - Home
- Underscore.JS - Overview
- Underscore.JS - Environment Setup
- Underscore.JS - Iterating Collection
- Underscore.JS - Processing Collection
- Underscore.JS - Iterating Array
- Underscore.JS - Processing Array
- Underscore.JS - Functions
- Underscore.JS - Mapping Objects
- Underscore.JS - Updating Objects
- Underscore.JS - Comparing Objects
- Underscore.JS - Utilities
- Underscore.JS - Chaining
- Underscore.JS Useful Resources
- Underscore.JS - Quick Guide
- Underscore.JS - Useful Resources
- Underscore.JS - Discussion
Underscore.JS - flatten method
Syntax
_.flatten(array, [shallow])
flatten method flattens a nested array where nesting can be upto any length. If shallow is passed as true then array will be flatten upto first level only.
Example
var _ = require('underscore');
var list = [1, [2], [4], 5, [[6]]]
//Example 1: flatten list
result = _.flatten(list);
console.log(result)
//Example 2: flatten list to first level only
result = _.flatten(list, true);
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, 4, 5, 6 ] [ 1, 2, 4, 5, [ 6 ] ]
underscorejs_processing_array.htm
Advertisements