Lodash _.toPath() Method

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.

Javascript




// Requiring the lodash library 
const _ = require("lodash");           
   
// Use of _.toPath() method
let gfg = _.toPath('Beginner.for.Beginner');
       
// Printing the output 
console.log(gfg);


Output:

["Beginner", "for", "Beginner"]

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.

Javascript




// Requiring the lodash library 
const _ = require("lodash");           
   
// Use of _.toPath() method
let gfg = _.toPath('Beginner[0].for[1].Beginner[2]');
       
// Printing the output 
console.log(gfg);


Output:

["Beginner", "0", "for", "1", "Beginner", "2"]

Contact Us