Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Basic Usage with Inherited Properties:
4.2.
JavaScript
4.3.
Iterating Over All Properties:
4.4.
JavaScript
4.5.
Combining with Other Lodash Methods:
4.6.
JavaScript
4.7.
Processing Inherited Data:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.entriesIn() differ from _.entries()?
5.2.
Can _.entriesIn() handle properties from deeply nested inheritance chains?
5.3.
Is it recommended to use _.entriesIn() for all objects?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.entriesIn() Method

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In JavaScript, when working with objects, sometimes it's necessary to consider not only the object's own properties but also the properties inherited from its prototype chain. Lodash provides a method for this purpose: _.entriesIn().

Lodash _.entriesIn() Method

This method retrieves an object's own and inherited enumerable string keyed properties as [key, value] pairs, making it useful for operations that require a comprehensive view of an object's properties, including those inherited.

Why This Function is Used

The _.entriesIn() function is used to convert an object into an array of its own and inherited enumerable properties as [key, value] pairs. This is particularly helpful in situations where you need to work with all the enumerable properties that an object has access to, including those it inherits, which can be important in more complex object-oriented programming scenarios or when dealing with objects created from extended prototypes.

Syntax, Parameter and Return Value

Syntax: 

_.entriesIn(object)

Parameters:

object (Object): The object to convert.

Return Value: 

(Array) - Returns the array of key-value pairs.

Examples 

Basic Usage with Inherited Properties:

  • JavaScript

JavaScript

var _ = require('lodash');

function Parent() {

 this.parentProp = 'parent value';

}

Parent.prototype.inheritedMethod = function() {};

var child = new Parent();

child.childProp = 'child value';

console.log(_.entriesIn(child));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

[['parentProp', 'parent value'], ['childProp', 'child value'], ['inheritedMethod', function() {}]]


Demonstrates including both own and inherited properties in the entries array.

Iterating Over All Properties:

  • JavaScript

JavaScript

var object = Object.create({ inheritedProp: 'inherited' });

object.ownProp = 'own';

_.entriesIn(object).forEach(([key, value]) => {

 console.log(`${key}: ${value}`);

});
You can also try this code with Online Javascript Compiler
Run Code

Output: 

'ownProp: own', 'inheritedProp: inherited'


Shows how to iterate over all properties, including inherited ones.

Combining with Other Lodash Methods:

  • JavaScript

JavaScript

var complexObject = Object.create({ inheritedProp: 42 });

complexObject.ownProp = 100;

var formattedEntries = _.chain(complexObject)

 .entriesIn()

 .map(([key, value]) => `${key}: ${value}`)

 .value();

console.log(formattedEntries);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

['ownProp: 100', 'inheritedProp: 42']


Demonstrates using _.entriesIn() in combination with other Lodash methods for custom formatting.

Processing Inherited Data:

  • JavaScript

JavaScript

function Shape() {

 this.type = 'shape';

}

Shape.prototype.displayType = function() { return this.type; };

var circle = new Shape();

circle.radius = 5;

var circleEntries = _.entriesIn(circle);

console.log(circleEntries);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

[['type', 'shape'], ['radius', 5], ['displayType', function() {}]]


An example of processing both own and inherited properties of an object.

Frequently Asked Questions

How does _.entriesIn() differ from _.entries()?

While _.entries() (or _.toPairs()) only retrieves an object's own properties, _.entriesIn() includes both the object's own and inherited properties.

Can _.entriesIn() handle properties from deeply nested inheritance chains?

Yes, _.entriesIn() will retrieve properties from the entire prototype chain, not just the immediate parent.

Is it recommended to use _.entriesIn() for all objects?

Use _.entriesIn() when you specifically need to consider inherited properties. For many use cases, especially with plain objects, _.entries() might be more appropriate to avoid unintentional inclusion of prototype properties.

Conclusion

Lodash's _.entriesIn() method is an effective tool for converting an object into an array of its own and inherited enumerable properties as [key, value] pairs. It's especially useful in object-oriented programming and in scenarios where a comprehensive view of an object's properties, including those from its prototype chain, is required.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass