Open In App

Lodash _.toPath() Method

Last Updated : 13 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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('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.

Javascript




// 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"]

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads