- Lodash Tutorial
- Lodash - Home
- Lodash - Overview
- Lodash - Environment Setup
- Lodash - Array
- Lodash - Collection
- Lodash - Date
- Lodash - Function
- Lodash - Lang
- Lodash - Math
- Lodash - Number
- Lodash - Object
- Lodash - Seq
- Lodash - String
- Lodash - Util
- Lodash - Properties
- Lodash - Methods
- Lodash Useful Resources
- Lodash - Quick Guide
- Lodash - Useful Resources
- Lodash - Discussion
Lodash - truncate method
Syntax
_.truncate([string=''], [options={}])
Truncates string if it's longer than the given maximum string length. The last characters of the truncated string are replaced with the omission string which defaults to "...".
Arguments
[string=''] (string) − The string to truncate.
[options={}] (Object) − The options object.
[options.length=30] (number) − The maximum string length.
[options.omission='...'] (string) − The string to indicate text is omitted.
[options.separator] (RegExp|string) − The separator pattern to truncate to.
Output
(string) − Returns the truncated string.
Example
var _ = require('lodash'); var result = _.truncate('Hi, this is a long text to be truncated', { 'length': 24, 'separator': ' ' }); console.log(result);
Save the above program in tester.js. Run the following command to execute this program.
Command
\>node tester.js
Output
Hi, this is a long...
lodash_string.htm
Advertisements