Saturday, October 25, 2025
HomeLanguagesJavascriptLodash _.toPlainObject() Method

Lodash _.toPlainObject() Method

The _.toPlainObject() method is used to convert the specified value to a plain object flattening inherited enumerable string keyed properties of value to own properties of the plain object.

Syntax:

_.toPlainObject(value)

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

  • value: This parameter holds a value to convert.

Return Value: This method returns the converted plain object.

Example 1:

Javascript




// Defining Lodash variable
const _ = require('lodash');
  
// Initializing a function gfg()
function gfg() {
    this.a = 1;
}
   
// Calling _.toPlainObject() function
console.log(_.assign({ 'b': 2 }, 
    _.toPlainObject(new gfg)));


Output:

{ b: 2, a: 1 }

Example 2:

Javascript




// Defining Lodash variable
const _ = require('lodash');
  
// Initializing a function gfg()
function gfg() {
     this.c = 4;
}
gfg.prototype.b = 2; 
  
// Calling _.toPlainObject() function
console.log( _.toPlainObject(new gfg) );


Output:

{ c: 4, b: 2 }

Note: This will not work in normal JavaScript as it requires the lodash library to be installed.

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS