Open In App

Underscore.js _.pairs() Function

Improve
Improve
Like Article
Like
Save
Share
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=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const obj = {
            Company: "GeeksforGeeks",
            Address: "Noida",
            Contact: "+91 9876543210",
            Email: "abc@gfg.com"
        }
        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=
    </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:



Last Updated : 15 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads