JavaScript Object fromEntries() Method
Last Updated :
19 Jun, 2023
The Object.fromEntries() method in JavaScript is a standard built-in object which is used to transform a list of key-value pairs into an object. This method returns a new object whose properties are given by the entries of the iterable.
Syntax:
Object.fromEntries( iterable )
Parameters: This method accepts a single parameter iterable which holds an iterable such as Array or Map or other objects implementing the iterable protocol.
Return value: This method always returns a new object whose properties are given by the entries of the iterable.
The below examples illustrate the Object.fromEntries() method in JavaScript:
Example 1: In this example, we will convert a Map into an Object using the Object.fromEntries() method in JavaScript.
javascript
const map1 = new Map([['big', 'small'], [1, 0]]);
const geek = Object.fromEntries(map1);
console.log(geek);
const map2 = new Map(
[['Geek1', 'Intern'],
['stipend', 'Works basis']]
);
const geek1 = Object.fromEntries(map2);
console.log(geek1);
Output:
Object { 1: 0, big: "small" }
Object { Geek1: "Intern", stipend: "Works basis" }
Example 2: In this example, we will convert an Array into an Object using the Object.fromEntries() method in JavaScript.
javascript
const arr1 = [['big', 'small'], [1, 0], ['a', 'z']];
const geek = Object.fromEntries(arr1);
console.log(geek);
const arr2 = [['Geek1', 'Intern'], ['stipend', 'Works basis']];
const geek1 = Object.fromEntries(arr2);
console.log(geek1);
Output:
Object { 1: 0, big: "small", a: "z" }
Object { Geek1: "Intern", stipend: "Works basis" }
Example 3: In this example, we will see some other Conversions to objects using Object.fromEntries() method in JavaScript.
javascript
const params = 'type=Get_the Value&geekno=34&paid=10';
const searchParams = new URLSearchParams(params);
console.log(Object.fromEntries(searchParams));
const object1 = { val1: 112, val2: 345, val3: 76 };
const object2 = Object.fromEntries(
Object.entries(object1)
.map(([key, val]) => [key, val * 3])
);
console.log(object2);
Output:
Object { type: "Get_the Value", geekno: "34", paid: "10" }
Object { val1: 336, val2: 1035, val3: 228 }
We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.
Supported Browsers: The browsers supported by Object.fromEntries() method are listed below:
- Google Chrome 73 and above
- Firefox 63 and above
- Opera 60 and above
- Safari 12.1 and above
- Edge 79 and above
Similar Reads
JavaTuples fromCollection() method The fromCollection() method in org.javatuples is used to instantiate a tuple in a semantically elegant way, with the values of the collection, given as parameters. This method can be used to any tuple class object of javatuples library. It is a static function in each javatuple class and it returns
2 min read
How to Learn Java Collections - A Complete Guide In the real world, a collection by definition is a group of articles that have similar properties and attributes. Since Java is an Object-Oriented Language it mimics the real world. In Java, a Collection is a group of multiple objects put together into a single unit. Java Collections is a very vast
15+ min read
Object Class in Java Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Ob
7 min read
Properties elements() method in Java with Examples The elements() method of Properties class is used to get the enumeration of this Properties object. It can be further used to retrieve the elements sequentially using this Enumeration received. Syntax: public Enumeration elements() Parameters: This method do not accepts any parameters.Returns: This
2 min read
Java Program to Access All Data as Object Array Java is an object-oriented programming language. Most of the work is done with the help of objects. We know that an array is a collection of the same data type that dynamically creates objects and can have elements of primitive types. Java allows us to store objects in an array. In Java, the class i
4 min read
JPA - Introduction to Query Methods In Java, JPA can defined as Java Persistence API. It can provide a powerful and intuitive way to interact with the database using object-oriented paradigms. Query Methods Can offer a convenient approach to define database queries directly within the repository and it can reduce the boilerplate code
5 min read
Java.util.Objects class in Java Java 7 has come up with a new class Objects that have 9 static utility methods for operating on objects. These utilities include null-safe methods for computing the hash code of an object, returning a string for an object, and comparing two objects.Using Objects class methods, one can smartly handle
7 min read
Classes and Objects in Java In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh
11 min read
AbstractSequentialList get() method in Java with Examples The get() method of AbstractSequentialList is used to fetch or retrieve an element at a specific index from a AbstractSequentialList. Syntax: AbstractSequentialList.get(int index) Parameters: The parameter index is of integer data type that specifies the position or index of the element to be fetche
2 min read
Properties keys() method in Java with Examples The keys() method of Properties class is used to get the enumeration of the keys in this Properties object. This enumeration can be used to traverse and iterate the keys sequentially. Syntax: public Enumeration keys() Parameters: This method accepts no parameters Returns: This method returns an Enum
2 min read