Lodash _.toPath() method is used to convert the given value to a property path array.
Syntax:
_.toPath(value)Parameters:
This method accepts a single parameter as mentioned above and described below:
- value: The value that need to convert to path array.
Return Value:
The new property path array.
Example 1: In this example, the code requires the Lodash library and employs the _.toPath method to convert a string with dot notation into an array representing a path, and then it displays the resulting path array in the console.
// Requiring the lodash library
const _ = require("lodash");
// Use of _.toPath() method
let gfg = _.toPath('geeks.for.geeks');
// Printing the output
console.log(gfg);
Output:
["geeks", "for", "geeks"]
Example 2: In this example, the code requires the Lodash library and utilizes the _.toPath method to convert a string with array index and object property notation into an array representing a path, and then it displays the resulting path array in the console.
// Requiring the lodash library
const _ = require("lodash");
// Use of _.toPath() method
let gfg = _.toPath('geeks[0].for[1].geeks[2]');
// Printing the output
console.log(gfg);
Output:
["geeks", "0", "for", "1", "geeks", "2"]