Sunday, April 5, 2026
HomeLanguagesJavascriptUnderscore _.toPath() Function

Underscore _.toPath() Function

The Underscore.js _.toPath() function is used to convert the given value to a property path array.
Syntax:

_.toPath('key')

Parameters: This method accepts a single parameter as mentioned above and described below.

    key: The key value that need to convert to path array.

Return Value: The new property path array.

Below example illustrates the  _.getPath() function is Underscore.js.

Example 1:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
  
        // Use of _.toPath() method 
        let gfg = _.toPath(['neveropen', 'for', 'neveropen']);
  
        // Printing the output  
        console.log(gfg);
    </script>
</body>
  
</html>


Output:

["neveropen","for","neveropen"]

Example 2:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
        var originalToPath = _.toPath;
        _.mixin({
            toPath: function (path) {
                return _.isString(path) ? 
                    path.split('.') : originalToPath(path);
            }
        });
        console.log({
            a: [{
                b: 5
            }]
        }, 'a.0.b');
    </script>
</body>
  
</html>


Output:

{"a":[{"b":5}]}
 a.0.b

Reference: https://underscorejs.org/#toPath

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32512 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6886 POSTS0 COMMENTS
Nicole Veronica
12007 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12100 POSTS0 COMMENTS
Shaida Kate Naidoo
7015 POSTS0 COMMENTS
Ted Musemwa
7259 POSTS0 COMMENTS
Thapelo Manthata
6972 POSTS0 COMMENTS
Umr Jansen
6960 POSTS0 COMMENTS