Warning: session_start(): open(/tmp/sess_853e247e0e9cb1dee32558adb39a9c48, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Convert An Object To A Map Using JavaScript - LearnShareIT

How To Convert An Object To A Map Using JavaScript

Convert an Object to a Map using JavaScript

In this article, we will know how to convert an object to a map using JavaScript with some methods. You can use map.set() or object.entries() function. Each approach is rather straightforward, and you can choose whichever you like.

Convert an object to a map using JavaScript

Using map.set()

You can use the set() method on a new map to set a new element in the map object. This element has two properties: “key” and “value” as these are the characters of a Map. 

Syntax:

set(key, value)

Parameters:

  • key: key of the element of the map which has to be unique.
  • value: the value of a map element with the key provided.

Example:

var map = new Map()
var object = {host : 'learnshareit.com'}

// set a map element with key is 'LEARNSHAREIT' and the value is the whole object
map.set('LEARNSHAREIT', object);

// set a map element which resembles the given object
map.set('host', object.host);
console.log(map);

Output:

Map(2) {
  'LEARNSHAREIT' => { host: 'learnshareit.com' },
  'host' => 'learnshareit.com'
}

The above example uses the set method to set new map elements. The first element has the key “LEARNSHAREIT” and its value is the whole object you have. The second element has the key “host” and has the value of the host property in the object. In fact, the second approach may be what most programmers are expecting. However, the above example has a disadvantage because you have to know the name (the key) of the element before setting it. To cope with this then function Object.keys(object) is very helpful:

Syntax:

Object.keys(object)

Parameter:

  • object: the object that you want to receive all the keys (returned in array)

Example:

var map = new Map()
var object = {host : 'learnshareit.com', aim : 'Learn and Share Knowledge', url : 'https://learnshareit.com'}

// print out an example of what Object.key() returns
console.log(Object.keys(object));

// iterate through an array of the object's keys
for (i of Object.keys(object))
    map.set(i, object[i]);

console.log(map);

Output: 

[ 'host', 'aim', 'url' ]
Map(3) {
  'host' => 'learnshareit.com',
  'aim' => 'Learn and Share Knowledge',
  'url' => 'https://learnshareit.com'
}

Using Object.entries()

This approach may be fast to convert an object to a map using JavaScript. The function will return an array of an array whose element is an element representing [key, value].

Syntax:

Object.entries(object)

Parameter:

  • object: the object you want to get the entries array.

First, we need an example to know exactly what the function will return:

var map = new Map()
var object = {host : 'learnshareit.com', aim : 'Learn and Share Knowledge', url : 'https://learnshareit.com'}

// print out an example of what Object.entries() returns
console.log(Object.entries(object));

Output: 

[
  [ 'host', 'learnshareit.com' ],
  [ 'aim', 'Learn and Share Knowledge' ],
  [ 'url', 'https://learnshareit.com' ]
]

As you can see, it returns an array in which each element is an array of 2 elements, the first element represents the object’s key and the second represents its corresponding value. To convert an object to a map, you just need to use the Map() constructor with the parameter is the array returned by the function Object.entries(object):

var object = {host : 'learnshareit.com', aim : 'Learn and Share Knowledge', url : 'https://learnshareit.com'}

// print out an example of what Object.key() returns
var map = new Map(Object.entries(object))
console.log(map);

Output: 

Map(3) {
  'host' => 'learnshareit.com',
  'aim' => 'Learn and Share Knowledge',
  'url' => 'https://learnshareit.com'
}

Summary

We have shown you how to convert an object to a map using JavaScript. It is helpful to consider that each method has its advantages and disadvantages. There are many posts related to this subject which you can read more such as convert an array to a set or convert a map into an array of object.

Maybe you are interested:

Leave a Reply

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