Skip to content

JavaScript Object fromEntries() Method

  • by

JavaScript Object fromEntries() Method allows you to easily create an object from an array of key-value pairs. This method is a relatively new addition to JavaScript, introduced in ECMAScript 2019.

Object.fromEntries(iterable)

This method takes an iterable such as an array and returns an object with properties corresponding to the key-value pairs. In this case, the keys are the first element of each inner array, and the values are the second element of each inner array.

JavaScript Object fromEntries() example

A simple example code has an array of key-value pairs, myArray, where each pair is an array itself. We then use the Object.fromEntries() method to convert this array of pairs into a single object, myObject.

<!DOCTYPE html>
<html>
<body>
    <script>
        const myArray = [
            ['key1', 'value1'],
            ['key2', 'value2'],
            ['key3', 'value3']
            ];

        const myObject = Object.fromEntries(myArray);

        console.log(myObject); 

    </script>
</body>
</html>

Output:

JavaScript Object fromEntries() Method

Do comment if you have any doubts or suggestions on this Js object method topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading