Lodash _.split() Method
Lodash _.split() method splits the given string by the given separator and up to the given limit length.
Syntax:
_.split(string, separator, limit);
Parameters:
- string: The string to split.
- separator: The separator by which the string is to be split.
- limit: The length to truncate results.
Return Value:
This method returns an array.
Example 1: In this example, we are splitting the given array by ‘-‘ by the use of the _.split() method.
Javascript
// Defining Lodash variable const _ = require( 'lodash' ); let str = "Beginner-for-Beginner" ; // Using _.replace() method console.log(_.split(str, '-' )) |
Output:
[ 'Beginner', 'for', 'Beginner' ]
Example 2: In this example, we are splitting the given array by ‘-‘ with limit of 2 by the use of the _.split() method.
Javascript
// Defining Lodash variable const _ = require( 'lodash' ); let str = "Beginner-for-Beginner" ; // Using _.replace() method console.log(_.split(str, '-' , 1)) |
Output:
[ 'Beginner' ]
Note: This will not work in normal JavaScript because it requires the lodash library to be installed and can be installed using npm install lodash.
Contact Us