Open In App

Lodash _.toPairs() Method

Last Updated : 27 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Lodash _.toPairs() method is used to create an array of enumerable string key-value pairs for an object that can be consumed by the _.fromPairs() function. If the object is a map or set, its entries are returned.

Syntax:

_.toPairs(object);

Parameter:

  • object: This parameter holds the object to query.

Return Value:

This method returns the array of key-value pairs.

Example 1: In this example, we are getting a pair of the given function by creating a prototype of it by the use of the lodash _.fill() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
function Fb() {
    this.id = 2045;
    this.username = 'fb_myself';
    this.password = 'fb1234';
}
 
// This will not be included in the array
Fb.prototype.email = 'myself@gmail.com';
 
// Use of _.toPairs() method
console.log(_.toPairs(new Fb));


Output:

[["id", 2045], ["username", "fb_myself"], ["password", "fb1234"]]

Example 2: In this example, we are getting a pair of the given object by the use of the lodash _.fill() method.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
let GfG = {
    'x': 1,
    'y': 2
}
 
// Use of _.toPairs() method
console.log(_.toPairs(GfG));


Output:

[['x', 1], ['y', 2]]

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

Similar Reads