Open In App

Underscore.js _.pairs() Function

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Underscore.js _.pairs() function is used to convert an object into an array of arrays that contain the [key, value] pairs of the object as elements.

Syntax:

_.pairs( object );

Parameters:

  • object: It contains the object element that holds the elements of key and value pair.

Return Value:

It returns the array of [key, value] pairs.

Example 1: The below code example implements the _.pairs() function of underscore.js practically.

html
<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
    </script>
</head>

<body>
    <script type="text/javascript">
        const obj = {
            Company: "GeeksforGeeks",
            Address: "Noida",
            Contact: "+91 9876543210",
            Email: "[email protected]"
        }
        console.log(_.pairs(obj)); 
    </script>
</body>

</html>

Output:

Example 2: The below code example illustrates the another use of _.pairs() function of underscore.js.

html
<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
    </script>
</head>

<body>
    <script type="text/javascript">
        const objectPair = _.pairs(
            { 
                num1: 10, 
                num2: 15, 
                num3: 20, 
                num4: 25, 
                num5: 30 
            }
        );
        console.log(objectPair); 
    </script>
</body>

</html>

Output:


Next Article

Similar Reads