D3.js | d3.map.set() Function

The map.set() function in D3.js used to set the values for the specified key string in to the created map.

Syntax:

d3.map.set(key, value);

Parameters: This function accepts two parameters which are illustrated below:

  • key: This is the key string.
  • value: This is the corresponding value for each key string.

Return Value: This function does not return any values.

Below programs illustrate the d3.map.set() function in D3.js:
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
   <title>d3.map.set() function</title>
  
   <script src = "https://d3js.org/d3.v4.min.js"></script>
</head>
     
<body>
    
  <script>
      
     // Creating a map
     var map = d3.map()
       
     // setting the value for the specified key string
     // into above map
     .set("a", 1).set("b", 2).set("c", 3);
  
     // Getting the the value for the specified key string
     A = map.get("a"); 
     B = map.get("c"); 
      
     // Printing the output of values
     console.log(A);
     console.log(B);
  </script>
</body>
  
</html>

chevron_right


Output:

1
3

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
  
<html>
  
<head>
   <title>d3.map.set() function</title>
  
   <script src = "https://d3js.org/d3.v4.min.js"></script>
</head>
  
<body>
    
  <script>
      
     // Creating a map and setting the value
     // for the specified key string
     var map = d3.map().set("Geeks", 1).set("Geek", 2).set("gfg", 3);
  
     // Getting the the value for the specified key string
     A = map.get("Geek"); 
     B = map.get("c"); 
      
     // Printing the output of values
     console.log(A);
     console.log(B);
  </script>
</body>
  
</html>

chevron_right


Output:

2
undefined

Note: In the above code, the string “c” is not present in the created map that is why undefined is printed as output.

Ref: https://devdocs.io/d3~5/d3-collection#map_set



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.