Search Here

Javascript Object.fromEntries() Method - Convert Array of Key-Value pair into an Object

Javascript Object.fromEntries() Method – Convert Array of Key-Value pair into an Object

Javascript Object.fromentries() method converts the iterable array to key, value pairs. This method does the opposite of Object.entries() method.

Syntax

Object.fromentries(object);

To convert a multi-dimensional array to an object the array must have another array and it must have two items first one will be its key and another will become its value.

var person = [
	["name" , "Sam"],
	["age" , 13],
];
var person_object = Object.fromEntries(person);

console.log("Person : ",person);
console.log("Person object created using fromEntries() method : ",person_object);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

//Console Output
Person :  (2) [Array(2), Array(2)]
Person object created using fromEntries() method :  {name: "Sam", age: 13}
Using Javascript Object.fromEntries() method

Using Javascript Object.fromEntries() method

We have a multi-dimensional array person which has all its items as an array. The first item is key and the second is its value.

Examples

var fruit = {"name" : "John", "age" : 10 };
var arr = Object.entries(fruit);
var reversed_arr = Object.fromEntries(arr);
console.log("Fruit object ",fruit);
console.log("Fruit object after applying entries() : ",arr);
console.log("Fruit object after applying Fromentries() : ",reversed_arr);

//Console Output
Fruit object  {name: "John", age: 10}
Fruit object after applying entries() :  (2) [Array(2), Array(2)]
Fruit object after applying Fromentries() :  {name: "John", age: 10}
Creating object from array using Object.fromEntries() method

Creating object from array using Object.fromEntries() method

There is another method Object.entries() which converts object to array in form of key, value pair and Object.fromEntries() method is reverse of Object.entries() method. It returns an object from an array key, value pair.

Conclusion

So it’s time to say bye we have come to the end of our post on Javascript Object.fromEntries() method – Convert Array of Key-Value pair into an Object. If you like our content then please do share it and comment to clarify your doubts.

Related Posts

Summary
Review Date
Reviewed Item
Javascript Object.fromEntries() Method - Convert Array of Key-Value pair into an Object
Author Rating
51star1star1star1star1star
Software Name
Javascript Programming
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Web Development