Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.toArray() method is used to convert the given value to an array.
Syntax:
_.toArray( value )
Parameters: This method accepts a single parameter as mentioned above and described below:
- value: This parameter holds the value to convert.
Return Value: This method returns the converted array.
Example 1:
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Use of _.toArray() method console.log(_.toArray( null )); console.log(_.toArray( 'gfg' )); |
Output:
[] ['g', 'f', 'g']
Example 2:
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Use of _.toArray() method console.log(_.toArray(10)); console.log(_.toArray({ 'html' : 1, 'css' : 2, 'javascript' : 3 })); |
Output:
[] [1, 2, 3]