Sunday, November 17, 2024
Google search engine
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

Most Popular

Recent Comments