Skip to content

JavaScript Map entries to Array | Example code

Use Array from() method to convert Map entries to Array in JavaScript. The entries() method returns a new iterator object that contains the [key, value] pairs for each element in the Map object in insertion order.

JavaScript Map entries to Array

Simple example code convert map in array using entires() and from() methods.

  <!DOCTYPE html>
  <html>
  <body>
    <script >
      const map1 = new Map();

      map1.set('0', 'foo');
      map1.set(1, 'bar');
      console.log(map1);

      const itr = map1.entries();

      console.log(Array.from(itr));

    </script>

  </body>
  </html>

Output:

JavaScript Map entries to Array

To create a Map from Object, we can convert an object into an array of [key, value] pairs and then create a Map using it.

let obj = {
	"name": "hackinbits",
	"type": "website"
}

To get an array of key-value pairs from the above object, we can use Object.entries().

let newArray = Object.entries(obj)

let map = new Map(newArray);

If we want to convert the Map in our previous example, back to Object it was created from(obj) we can use Object.fromEntries() method.

Do comment if you have any doubts or suggestions on this Js map 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

Your email address will not be published. Required fields are marked *